json fieldnames spaces

拟墨画扇 提交于 2019-12-01 14:43:59

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5716792/json-fieldnames-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!