Lopoc
Lopoc

Reputation: 1358

json fieldnames spaces

I've such a json structure:

info:
{
First Name: "Robert",
Last Name: "Smith"
}

I'm tring to point to data with javascript using something like: "info.First Name" I know it's incorrect. How can I retrieve those information from the structure I have?

thank

Upvotes: 10

Views: 34971

Answers (1)

Andy E
Andy E

Reputation: 344665

That's not valid JSON. JSON is a data transport format that requires field names to be string delimited with double quotes, e.g.

{
    "info" : {
        "First Name": "Robert",
        "Last Name": "Smith"
    }
}

After parsing, you can then use obj.info["First Name"] to access the First Name field.

What you have is a JS object literal (that's still invalid), but you can apply the same technique (stringify the property names) to reach the same end goal.

Upvotes: 36

Related Questions