yety
yety

Reputation: 711

Wrong JSON encoding - PHP cant read it

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?

Upvotes: 0

Views: 155

Answers (3)

czerasz
czerasz

Reputation: 14250

Try this:

[
  {
    "name": "John Doe",
    "birthYear": "1990",
    "reqion": "USA",
    "phone": "604789577"                 
  },
  {
    "name": "Jose Dirack",
    "birthYear": "1970",
    "reqion": "Europe",
    "phone": "768789577"                 
  }
]

Upvotes: 0

Gumbo
Gumbo

Reputation: 655239

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.

Upvotes: 3

alex
alex

Reputation: 490233

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.

Upvotes: 1

Related Questions