如何使用javascript从JSON访问单个对象?

时间:2015-03-29 16:34:30

标签: javascript json siblings

我有一个JSON列表来自具有多个兄弟姐妹的数据库中的表。

"PlayerNumbers":{"PlayerNumber":[
{"Section":"1","Team":"1","Direction":"N","Number":"00280149","Name":" ANSART A","Updated":"False","TimeLog":"","Processed":"False"},
{"Section":"1","Team":"1","Direction":"S","Number":"00280131","Name":"ANSART Y","Updated":"False","TimeLog":"","Processed":"False"},
{"Section":"1","Team":"2","Direction":"N","Number":"01964990","Name":" LAHAYE B","Updated":"False","TimeLog":"","Processed":"False"}]}

说我需要阅读"名称" key = 1,Team = 2,Direction = N。

编辑:我当然会使用js解析对象

Javascript中的语法是什么?

2 个答案:

答案 0 :(得分:2)

我会假设您根本没有处理JSON,但是使用JSON中的对象树解析。而且我将假设你给出的是对象定义的内容,比如这个JSON:

{
    "PlayerNumbers": {
        "PlayerNumber": [
            {
                "Section": "1",
                "Team": "1",
                "Direction": "N",
                "Number": "00280149",
                "Name": " ANSART A",
                "Updated": "False",
                "TimeLog": "",
                "Processed": "False"
            },
            {
                "Section": "1",
                "Team": "1",
                "Direction": "S",
                "Number": "00280131",
                "Name": "ANSART Y",
                "Updated": "False",
                "TimeLog": "",
                "Processed": "False"
            },
            {
                "Section": "1",
                "Team": "2",
                "Direction": "N",
                "Number": "01964990",
                "Name": " LAHAYE B",
                "Updated": "False",
                "TimeLog": "",
                "Processed": "False"
            }
        ]
    }
}

因此,在解析了JSON之后,您将拥有一个具有PlayerNumbers属性的对象,该对象引用具有PlayerNumber属性的对象,该属性引用具有该信息的对象数组你想要的。

所以你循环遍历数组,直到找到第一个匹配的条目(假设你只想要第一个):

var name;
obj.PlayerNumbers.PlayerNumber.some(function(entry) {
    if (entry.Section == "1" && Team == "2" && entry.Direction == "N") {
        name = entry.Name;
        return true;
    }
    return false;
});

如果您想要所有匹配项:

var names = obj.PlayerNumbers.PlayerNumber.filter(function(entry) {
    return (entry.Section == "1" && Team == "2" && entry.Direction == "N");
}).map(function(entry) { return entry.Name; });

以上两种都使用过时引擎上缺少Array的ES5功能。如果您需要支持过时的引擎,以上所有内容都可以" shimmed"或" polyfilled,"搜索应该打开必要的垫片/填充物。

答案 1 :(得分:1)

var playersObject = { 
    "PlayerNumbers": {
    "PlayerNumber": [{
        "Section": "1",
        "Team": "1",
        "Direction": "N",
        "Number": "00280149",
        "Name": " ANSART A",
        "Updated": "False",
        "TimeLog": "",
        "Processed": "False"
    }, {
        "Section": "1",
        "Team": "1",
        "Direction": "S",
        "Number": "00280131",
        "Name": "ANSART Y",
        "Updated": "False",
        "TimeLog": "",
        "Processed": "False"
    }, {
        "Section": "1",
        "Team": "2",
        "Direction": "N",
        "Number": "01964990",
        "Name": " LAHAYE B",
        "Updated": "False",
        "TimeLog": "",
        "Processed": "False"
    }]
    }
};

   function getParticularPlayer(section, team, direction) {
     return playersObject.PlayerNumbers.PlayerNumber.filter(function(player) {
            return (player.Section===section && player.Team===team && player.Direction===direction); 
     });
   }

现在,您可以使用

   var results = getParticularPlayer("1","1","N");

它将返回一系列匹配的玩家。如果您确定只返回一个玩家,则可以使用results[0]