在json数据中获取嵌套映射

时间:2013-04-10 06:41:04

标签: javascript jquery json

我有JSON数据,例如:

var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}'

它实际上是以JSON的形式返回给我的。我只是在这里展示它,例如清酒。我可以使用data["credit"]来获取名为credit的元素,但这只会让我:

{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}

我如何深入一击? data["credit"]["credit"]语法不正确。它导致未定义。我知道我很接近但不记得其余的。目标是从JSON数据中获取149.96

3 个答案:

答案 0 :(得分:1)

现在您可以使用alert(data.credit.credit)

        var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}';
        eval("data=" + data);
        alert(data.credit.credit);

您也可以使用data = JSON.parse(data);将字符串转换为Json,但这会导致浏览器兼容性问题。

这些是支持

的浏览器
  • Firefox(Mozilla)3.5
  • Internet Explorer 8
  • Opera 10
  • 列表项
  • Safari 4

旧版本不支持。

答案 1 :(得分:0)

使用.运算符来获取对象的对象

alert(data.credit.credit) //gives 149.96
 alert(data.credit.amount) //gives 149.96
alert(data.credit.card) //gives NT2C8FFC

答案 2 :(得分:0)

您正在使data字符串不是对象。变化

var data = '{"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}'

var data = {"credit":{"@code":"NT2C8FFC","card":"NT2C8FFC","credit":"149.96","amount":"149.96","disabled":"0","expired":"2011-02-15 10:21:18","user":"xxxx","issued":"2010-02-15 10:21:18","orderid":[],"fromorder":[],"email":"xxxx","phone":[],"state":[],"address":[],"city":[],"zip":[],"country":[],"customerid":"xxx","order":"xxx","order_date":"2010-01-23 00:00:00","reason":"Product Not Working as Expected","source":"xxx","first":[],"last":[],"notes":[]}}

如果您无法更改原始数据,则需要执行

解析
data = JSON.parse(data);

(适用于大多数浏览器)或使用JQuery's等库中的方法:

data = jQuery.parseJSON(data);