JavaScript json值 [英] JavaScript json values

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

问题描述

使用JavaScript;我如何产生此输出

Using JavaScript; how do I produce this output

cell
work (123) 456 7890

来自此有效json

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

推荐答案

从狭义上讲,您需要这样做

In a very narrow sense, you'd need to do this

var jsonObj = JSON.parse(jsonString);

var type = jsonObj.phone[0]['@attributes'].type // "cell"
var phoneNumber = jsonObj.phone[1] // "(123) 456 7890"

但是该JSON数据的结构可能会发生变化,因此您不能依赖0/1索引,并且无论如何该结构都是怪异的.就像评论中所说的那样,如果它来自XML,请解析它

But the structure of that JSON data may change, so you can't rely on the 0/1 indexes, and anyway the structure is weird. Like the comments say, if it comes from XML, then parse that instead

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

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