search

Home  >  Q&A  >  body text

javascript - 如何获取json对象中的特定值?

{
    "id": "some",
    "title": "some",
    "author": {
        "name": "\u95eb\u9759"
    },
    "link": [{
        "@attributes": {
            "href": "http://localhost/",
            "rel": "some"
        }
    },
    {
        "@attributes": {
            "href": "http://localhost/",
            "rel": "some"
        }
    },
    {
        "@attributes": {
            "href": "http://localhost/",
            "rel": "some"
        }
    }]
}

如上,对于服务器返回的json数据,如何获取Link中第一个@attributes的href值?

PHPzPHPz2790 days ago868

reply all(4)I'll reply

  • 数据分析师

    数据分析师2017-10-01 01:13:33

    javascript - How to get specific value in json object? -PHP Chinese Q&A-javascript - How to get a specific value in a json object? -PHP Chinese website Q&A

    Let’s take a look and learn.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-10 12:43:07

    var req = { ...你的返回对象... };
    var first_href_val = req.link[0]['@attributes'];

    reply
    0
  • 迷茫

    迷茫2017-04-10 12:43:07

    There is the json site(http://www.json.org/), it lists the tutorial of all languages supporting json.
    May be it is helpful.

    for python(http://docs.python.org/library/json.h...)
    for ruby(http://flori.github.com/json/)

    reply
    0
  • 高洛峰

    高洛峰2017-04-10 12:43:07


    function getJsonValue(obj,name){ var result = null; var value = null; for(var key in obj){ value = obj[key]; if(key == name){ return value; } else { if( typeof value == "object" ){ result = getJsonValue(value,name); } } } return result; }

    采用递归方式来查找josn中的你需要的key.

    但是还不支持 数组,你可以自己扩充,思路就是这样!

    var taskId = getJsonValue(jsonobj,"history");
    console.log(taskId);
    var jsonobj = { "semantic":{"taskId":"8.4.3"},"history":"cn.xxxx.fund"};
    

    reply
    0
  • Cancelreply