简体   繁体   English

如何从JavaScript中的嵌套JSON解析元素?

[英]How to parse elements from nested JSON in javascript?

I have the following json which gets returned as responseText after I send a GET request to a rest api: 我将GET请求发送到rest api后,将以下json作为responseText返回:

{
    "ehrs": [
        {
            "uid": "11111111-1111-1111-1111-111111111111",
            "dateCreated": "2017-09-21 04:36:47",
            "subjectUid": "11111111-1111-1111-1111-111111111111",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "22222222-1111-1111-1111-111111111111",
            "dateCreated": "2017-09-21 04:36:47",
            "subjectUid": "22222222-1111-1111-1111-111111111111",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "33333333-1111-1111-1111-111111111111",
            "dateCreated": "2017-09-21 04:36:48",
            "subjectUid": "33333333-1111-1111-1111-111111111111",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "44444444-1111-1111-1111-111111111111",
            "dateCreated": "2017-09-21 04:36:48",
            "subjectUid": "44444444-1111-1111-1111-111111111111",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "55555555-1111-1111-1111-111111111111",
            "dateCreated": "2017-09-21 04:36:48",
            "subjectUid": "55555555-1111-1111-1111-111111111111",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "97fecfa9-d51c-440f-93eb-cb8bbabc5cdc",
            "dateCreated": "2017-09-27 22:53:55",
            "subjectUid": "ef70c3bf-8aba-4f4b-83b7-097f2aff60f6",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "4e363a7c-0b25-405c-93d8-1361f4775ccd",
            "dateCreated": "2017-10-10 06:08:58",
            "subjectUid": "228084fb-10d8-4441-8683-aad526e2c5fd",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        },
        {
            "uid": "3c6b8a98-1488-427b-8251-5bebad35afda",
            "dateCreated": "2017-10-10 21:03:44",
            "subjectUid": "3dd3a7b1-b642-4e9b-b12a-4fcd10af6e4f",
            "systemId": "CABOLABS_EHR_SERVER",
            "organizationUid": "123456"
        }
    ],
    "pagination": {
        "max": 10,
        "offset": 0,
        "nextOffset": 10,
        "prevOffset": 0
    },
    "timing": "0 ms"
}

I want to fetch only "uid" and "dateCreated" elements from all the "ehrs" elements of this json, and then display them in a proper manner with both attribute name and its value shown. 我只想从此json的所有“ ehrs”元素中获取“ uid”和“ dateCreated”元素,然后以适当的方式显示它们,并同时显示属性名称及其值。 I tried the following code: 我尝试了以下代码:

var ehrjson = (this.responseText);
var ehrobj = JSON.parse(ehrjson);
document.getElementById("ehrlist").innerHTML = ehrobj.ehrs.uid+"    ----     "+ehrobj.ehrs.dateCreated+"\n";

But it is showing [undefined] ---- [undefined] as output in html. 但是它显示[undefined] ---- [undefined]作为html输出。 I too have a feeling that my code is wrong but can't figure what to do. 我也有一种感觉,我的代码是错误的,但是不知道该怎么做。 How to approach this problem? 如何解决这个问题?

after ehrobj = JSON.parse(ehrjson) you obtain an object with a ehrs property that is an array ehrobj = JSON.parse(ehrjson)您将获得一个带有ehrs属性的对象,该对象是一个数组

you can only access the properties uid and dateCreated for individual elements of the array. 您只能访问数组的各个元素的属性uiddateCreated

So you either (1) can display a single element properties uid and dateCreated 因此,您(1)可以显示一个元素属性uiddateCreated

ehrobj.ehrs[0].uid+" "+ehrobj.ehrs[0].dateCreated

or (2) you may get them all concatenated 或(2)您可以将它们全部串联在一起

ehrobj.ehrs.map(e => e.uid + " " + e.dateCreated).join("\n")

Your json parsing is not the problem here. 您的json解析不是这里的问题。 You have to access each array element of ehrobj.ehrs and cycle them like in this snippet 您必须访问ehrobj.ehrs的每个数组元素,并像此代码段一样循环它们

 // ehrobj as js-object to get this snippet running var ehrobj = { "ehrs": [ { "uid": "11111111-1111-1111-1111-111111111111", "dateCreated": "2017-09-21 04:36:47", "subjectUid": "11111111-1111-1111-1111-111111111111", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "22222222-1111-1111-1111-111111111111", "dateCreated": "2017-09-21 04:36:47", "subjectUid": "22222222-1111-1111-1111-111111111111", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "33333333-1111-1111-1111-111111111111", "dateCreated": "2017-09-21 04:36:48", "subjectUid": "33333333-1111-1111-1111-111111111111", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "44444444-1111-1111-1111-111111111111", "dateCreated": "2017-09-21 04:36:48", "subjectUid": "44444444-1111-1111-1111-111111111111", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "55555555-1111-1111-1111-111111111111", "dateCreated": "2017-09-21 04:36:48", "subjectUid": "55555555-1111-1111-1111-111111111111", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "97fecfa9-d51c-440f-93eb-cb8bbabc5cdc", "dateCreated": "2017-09-27 22:53:55", "subjectUid": "ef70c3bf-8aba-4f4b-83b7-097f2aff60f6", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "4e363a7c-0b25-405c-93d8-1361f4775ccd", "dateCreated": "2017-10-10 06:08:58", "subjectUid": "228084fb-10d8-4441-8683-aad526e2c5fd", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" }, { "uid": "3c6b8a98-1488-427b-8251-5bebad35afda", "dateCreated": "2017-10-10 21:03:44", "subjectUid": "3dd3a7b1-b642-4e9b-b12a-4fcd10af6e4f", "systemId": "CABOLABS_EHR_SERVER", "organizationUid": "123456" } ], "pagination": { "max": 10, "offset": 0, "nextOffset": 10, "prevOffset": 0 }, "timing": "0 ms" }; ehrobj.ehrs.forEach(function (ehr) { document.getElementById("ehrlist").innerHTML += ehr.uid + " ---- " + ehr.dateCreated + "\\n"; }); 
 <pre id="ehrlist"></pre> 

Note that ehrobj.ehrs is an array, so ehrobj.ehrs.uid is not a property of his. 请注意,ehrobj.ehrs是一个数组,因此ehrobj.ehrs.uid不是其属性。

    ehrobj.ehrs === [
    {
        "uid": "11111111-1111-1111-1111-111111111111",
        "dateCreated": "2017-09-21 04:36:47",
        "subjectUid": "11111111-1111-1111-1111-111111111111",
        "systemId": "CABOLABS_EHR_SERVER",
        "organizationUid": "123456"
    }, ... ]

You need to iterate for ehrs array. 您需要迭代ehrs数组。

let ehrlist = document.getElementById( 'ehrlist' );
ehrobj.ehrs.forEach( function(ehr){
   let newEl = document.createElement('div');
   newEl.innerHTML = ehr.uid + ' ------ ' + ehr.dateCreated;
   ehrlist.appendChild( newEl );
} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM