如何在JSON中访问变量

时间:2012-01-04 21:48:34

标签: javascript json parsing

我有以下JSON输出,我需要访问变量TN_TEXTO。我该如何访问它?

["data", [
    ["notification",
    {
        "TN_CODIGO": "3",
        "TN_TP_CODIGO": "1",
        "TN_TEXTO": "dddddddddddd",
        "TN_DATA": "1325708743",
        "TN_LINK": "",
        "TN_READ": "0"
    }]
]]

提前致谢!

3 个答案:

答案 0 :(得分:0)

使用JSON.parse将字符串转换为对象。然后你可以在上面使用JavaScript语法。

var object = JSON.parse(string);
alert(obj[1][0][1].TN_TEXTO);

Live example

答案 1 :(得分:0)

myvar = ["data", [
    ["notification",
    {
        "TN_CODIGO": "3",
        "TN_TP_CODIGO": "1",
        "TN_TEXTO": "dddddddddddd",
        "TN_DATA": "1325708743",
        "TN_LINK": "",
        "TN_READ": "0"
    }]
]]


alert(myvar[1][1]["TN_CODIGO"]); // is 3

答案 2 :(得分:0)

如果您使用的是JSON字符串:

var json = '["data", [["notification", { "TN_CODIGO": "3", "TN_TP_CODIGO": "1", "TN_TEXTO": "dddddddddddd", "TN_DATA": "1325708743", "TN_LINK": "", "TN_READ": "0" }] ]]';
var array = JSON.parse(json);
alert(array[1][0][1]["TN_TEXTO"]);

如果您已经解析了字符串:

alert(array[1][0][1]["TN_TEXTO"]);