Access json value using JavaScript object notation

后端 未结 4 805
囚心锁ツ
囚心锁ツ 2021-01-28 18:34

I am unable to access a json value

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


        
4条回答
  •  情话喂你
    2021-01-28 19:14

    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('
    '+json['phone'][1]); console.log('
    '+json['phone'][2]['@attributes'].type);

    DEMO

提交回复
热议问题