I have this JSON file
{
1 : {
name: "John Doe",
birthYear: "1990",
reqion: "USA"
phone: "604789577",
},
2 : {
name: "Jose Dirack",
birthYear: "1970",
reqion: "Europe"
phone: "768789577",
}
}
And json_decode() is uanble to decode it.
Do you see why? Have you any idea how to fix it?
Mat
208k41 gold badges409 silver badges423 bronze badges
3 Answers 3
The keys in objects need to be properly encoded strings:
{
"1" : {
"name": "John Doe",
"birthYear": "1990",
"reqion": "USA",
"phone": "604789577"
},
"2" : {
"name": "Jose Dirack",
"birthYear": "1970",
"reqion": "Europe",
"phone": "768789577"
}
}
There was also a typo with the separating commas.
answered Feb 8, 2012 at 10:37
Gumbo
657k112 gold badges792 silver badges852 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Timothy Groote
Careful now, there were still some small problems (see my edit)
T.J. Crowder
+1 for showing the corrected version and linking to the little-known RFC.
Gumbo
@alex You would just had scrolled down the page you have linked to. There is also a link to the RFC 4627 application/json (although just the plain text format).
alex
@Gumbo I shall pay more attention to site's footers from here on in.
The keys must be quoted with double quotes as per the JSON spec. If the outer object is meant to be an array, swap the {} with [] and drop the explicit numbering.
You are also missing a comma after reqion.
You also have trailing commas which shouldn't be there.
answered Feb 8, 2012 at 10:36
alex
492k205 gold badges891 silver badges992 bronze badges
1 Comment
T.J. Crowder
Yeah, Crockford really blew it by not allowing trailing commas.
Try this:
[
{
"name": "John Doe",
"birthYear": "1990",
"reqion": "USA",
"phone": "604789577"
},
{
"name": "Jose Dirack",
"birthYear": "1970",
"reqion": "Europe",
"phone": "768789577"
}
]
answered Feb 8, 2012 at 10:38
czerasz
14.4k10 gold badges56 silver badges63 bronze badges
Comments
lang-php
reqion: "USA" phone: "604789577",is missing a comma