你能告诉我如何在javascript中读取json吗?
我有一个如下所示的 json 字符串
{"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}
我想读取Skype电话号码
最佳答案
如果您从 JSON 字符串开始,请从 1
开始。如果您已有 JS 对象,请跳到 2
。
使用
JSON.parse()
解析字符串将 JSON 字符串转换为 JS 对象。支持a browser that does not have the native JSON ,您可以使用Crockford's JSON2 library来实现它。var jsondata = JSON.parse('{"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}');
像平常一样从 JS 对象中检索值
var skype = jsondata.person['phone number'].skype;
这是完整的代码和 a sample :
var jsondata = JSON.parse('{"person":{"First name":"Dharmalingm","Last name":"Arumugam","Address":{"door number":"123","street":"sample street","city":"sample_city"},"phone number":{"mobile":"0123","landline":"01234","skype":"01235"}}}');
//normally, the dot-notation is used
//but since "phone number" is not a valid key when using dot-notation
//the bracket notation is used
var skype = jsondata.person['phone number'].skype;
关于php - 如何在javascript中读取json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727841/