使用JavaScript对象表示法访问json值 [英] Access json value using JavaScript object notation

查看:109
本文介绍了使用JavaScript对象表示法访问json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法访问json值

{"phone": [
{
    "@attributes": {
        "type": "cell",
        "ext": ""
    }
}, "(123) 456 7890", {
    "@attributes": {
        "type": "work",
        "ext": ""
    }
}
]}

使用以下JavaScript:psudo

using the following JavaScript: psudo

for each phone line ...

console.log(["@attributes"].type);
console.log(this); 
console.log(["@attributes"].ext);

... end for

我预计以下输出:

cell
work (123) 456 7890


推荐答案

实际上你的json结构并不完美,所以这里是你想要的输出的解决方案

actually your json structure is not perfect, so here is the solution for your desired output

var json = {"phone": [
{
    "@attributes": {
        "type": "cell",
        "ext": ""
    }
}, "(123) 456 7890", {
    "@attributes": {
        "type": "work",
        "ext": ""
    }
}
]};
 console.log(json['phone'][0]['@attributes'].type);
 console.log('<br/>'+json['phone'][1]);
 console.log('<br/>'+json['phone'][2]['@attributes'].type);

这篇关于使用JavaScript对象表示法访问json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆