json值获取问题

{
    "response":{
        "110":{
            "location":null,
            "name":"报警电话",
            "type":"poi"
        }
    },
    "responseHeader":{
        "status":200,
        "time":1452220774806,
        "version":"1.1.0"
    }
}

上述的json如何快速获取location、name、type的值,110表示的是用户输入的号码,是会变化的。

阅读 2.2k
2 个回答

变与不变并没有区别。
变就定义成变量就行了。

var foo = 110; // 或者从用户那儿取到这个数据

var location = response[foo].location;
var name = response[foo].name;
var type = response[foo].type;
var data = {
    "response":{
        "110":{
            "location":null,
            "name":"报警电话",
            "type":"poi"
        }
    },
    "responseHeader":{
        "status":200,
        "time":1452220774806,
        "version":"1.1.0"
    }
};

$.each(data, function (index, value) { 
    if ("response" == index) { 
        $.each(value, function (index, value) { 
            console.log(index, value);
        }); 
    }
    return true;
});

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题