php - 如何在javascript中读取json?

标签 php javascript json

你能告诉我如何在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

  1. 使用 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"}}}');
    
  2. 像平常一样从 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/

相关文章:

php - 使用 jQuery/MySQL 刷新文本区域

c# - 将 JSON 反序列化为 C#

ios - 以前的 NSDictionary 现在到 JSON 数组

php - 类 stdClass 的对象无法转换为 int Laravel 5.6

php - 我很困惑,构建 MVC 或 MVVM 设计模式的真正方法是什么?

php - 在哪里可以找到将 Twitter 集成到我的站点的代码?

javascript - D3强制布局: how to update link color?

javascript - 为什么 setInterval 回调只执行一次?

javascript - 在 jQueryUI 对话框中显示和格式化数组元素

json - 无法将类为 'parseDate' 的对象 'java.lang.String' 转换为 grails 中的类 'java.util.Date'