Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to decode this array of object to Php array, I used json_decode() but that returned Null

$a = "[
{
    id:1,
    name:'rajan',
    class:10
},{
    id:2,
    name:'amrit',
    class:12
},{
    id:3,
    name:'arun',
    class:11
}
]";
share|improve this question
    
Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems have you run into? You have a string right now - so it is not decodable. –  Jay Blanchard Jan 29 at 14:47
1  
The lack of quote marks around key values such as id, name and class suggests that this is not valid json –  Mark Baker Jan 29 at 14:52
    
I tried to decode above string using json_decode, but that does not worked out!! I need to know how can I convert above string to php array –  Rajan Jan 29 at 14:55
    
@MarkBaker Yes, I know there is no quotes on the key of each objects, that's the reason I am getting null on the json_decode but the key of the json object may not be Quoted thats doesnot mean thats invalid json!!! am I right?? I have to decode in such case :( I spend couple of hour on it!! –  Rajan Jan 29 at 14:59
    
So where is this json coming from? The optimal solution is to fix it at source, not to put in some workround/monkeypatch to resolve it.... and RFC4627 is quite clear about the need for quote marks: The representation of strings is similar to conventions used in the C family of programming languages. __A string begins and ends with quotation marks.__ –  Mark Baker Jan 29 at 15:19

1 Answer 1

up vote 4 down vote accepted

Your JSON is not valid, which is why json_decode is returning null.

SyntaxError: Unexpected token i

http://json.parser.online.fr/

http://php.net/manual/en/function.json-decode.php

Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

I have also fixed your JSON:

[
{
    "id": 1,
    "name":"rajan",
    "class":10
},{
    "id":2,
    "name":"amrit",
    "class":12
},{
    "id":3,
    "name":"arun",
    "class":11
}
]

Fixing your broken JSON with code:

$replace_keys = preg_replace("/(\w*):/i", '"$1":', $a);
$fix_values = preg_replace("/:'(\w*)'/i", ':"$1"', $replace_keys);

You can then use $replace_keys in json_decode. I am not sure if this is the best method, as small variations in your json could break it, but it fixes it with the example provided.


Full code to fix JSON:

$a = "[
{
    id:1,
    name:'rajan',
    class:10
},{
    id:2,
    name:'amrit',
    class:12
},{
    id:3,
    name:'arun',
    class:11
}
]";


$replace_keys = preg_replace("/(\w*):/i", '"$1":', $a);
$fix_values = preg_replace("/:'(\w*)'/i", ':"$1"', $replace_keys);

die($fix_values);
share|improve this answer
    
Not really an answer to his question. –  Daan Jan 29 at 14:49
    
@Daan Answer has been expanded. –  Jono20201 Jan 29 at 14:51
    
I know I upvoted. –  Daan Jan 29 at 14:54
    
its actually its array of object, object may not have quotes around the keys!! and in the case what I supposed to do, wrap the key with the quotes and then use json_decode :( –  Rajan Jan 29 at 15:10
    
I know If I add the Quotes its will work, but that's not the actual solution is it??? –  Rajan Jan 29 at 15:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.