如何使用Javascript从JSON提取值?

时间:2018-06-20 17:17:47

标签: javascript json

对不起,我在这里不知所措。我没有关于Javascript的线索。我正在尝试根据我在网上可以找到的东西。同时,如果有任何人可以在这里帮助我,那就太好了。

请参阅下面的JSON。我已将其缩减为仅3个项目,但可能约为50个。如果customFieldId为3,则我希望提取ID 7514;如果customFieldId为1,则希望提取3854。

{
"items": [
    {
        "id": 3854,
        "customFieldId": 1,
        "customField": {
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/1"
                }
            ]
        },
        "value": "Yes",
        "links": [
            {
                "rel": "canonical",
                "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/3854"
            }
        ]
    },
    {
        "id": 7514,
        "customFieldId": 3,
        "customField": {
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/3"
                }
            ]
        },
        "value": "No",
        "links": [
            {
                "rel": "canonical",
                "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/7514"
            }
        ]
    },
    {
        "id": 93432,
        "customFieldId": 10,
        "customField": {
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/10"
                }
            ]
        },
        "value": "Fulltime-Regular",
        "links": [
            {
                "rel": "canonical",
                "href": "https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/93432"
            }
        ]
    }
]

}

3 个答案:

答案 0 :(得分:1)

您可以使用Array.find()

var obj = {"items" :[{"id":3854,"customFieldId":1,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/1"}]},"value":"Yes","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/3854"}]},{"id":7514,"customFieldId":3,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/3"}]},"value":"No","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/7514"}]},{"id":93432,"customFieldId":10,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/10"}]},"value":"Fulltime-Regular","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/93432"}]}]};

var customId = 3;
var result = obj.items.find((obj)=> obj.customFieldId === customId);
console.log(result);

答案 1 :(得分:1)

您可以编写一个简单的循环,检查customFieldId是3还是1,如果是,则为您获取fieldId。

for(let i = 0; i < myObj.items.length; i++) {
 let fieldId = myObj.items[i].customFieldId;
 let id = myObj.items[i].id;

 if(fieldId === 3 || fieldId === 1) {
  console.log("The custom field ID is: " + fieldId + " and the id is: " + id);
 }
}

答案 2 :(得分:1)

您可以使用过滤器功能从JSON过滤数据

var obj = {"items" :[{"id":3854,"customFieldId":1,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/1"}]},"value":"Yes","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/3854"}]},{"id":7514,"customFieldId":3,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/3"}]},"value":"No","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/7514"}]},{"id":93432,"customFieldId":10,"customField":{"links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/learnCenters/178410/userCustomFields/10"}]},"value":"Fulltime-Regular","links":[{"rel":"canonical","href":"https://stgxilinx.learn.taleo.net/learn.rest/v1/memberships/3487/customFieldValues/93432"}]}]};

function customFieldId(obj) {
   return obj.customFieldId == 3;
}

var result = obj.items.filter(customFieldId);
console.log(result);