简体   繁体   中英

How to extract data from json nested objects?

I have JSON file as below and I want to extract placeName's valus which is "Ince Minaret Medrese" and "Karatay, Konya" and lat's and long's values which are 37.8728 and 32.49 and for "Karatay, Konya" lat's value is 37.8667 and long's value is 32.5289

{
    "head": {
        "link": [],
        "vars": ["placeName", "lat", "long"]
    },
    "results": {
        "distinct": false,
        "ordered": true,
        "bindings": [{
                "placeName": {
                    "type": "literal",
                    "xml:lang": "en",
                    "value": "Ince Minaret Medrese"
                },
                "lat": {
                    "type": "typed-literal",
                    "datatype": "http://www.w3.org/2001/XMLSchema#float",
                    "value": "37.8728"
                },
                "long": {
                    "type": "typed-literal",
                    "datatype": "http://www.w3.org/2001/XMLSchema#float",
                    "value": "32.49"
                }
            },
            {
                "placeName": {
                    "type": "literal",
                    "xml:lang": "en",
                    "value": "Karatay, Konya"
                },
                "lat": {
                    "type": "typed-literal",
                    "datatype": "http://www.w3.org/2001/XMLSchema#float",
                    "value": "37.8667"
                },
                "long": {
                    "type": "typed-literal",
                    "datatype": "http://www.w3.org/2001/XMLSchema#float",
                    "value": "32.5289"
                }
            }
        ]
    }
}

I tried to do it this way

    <p id="demo"></p>

    <script>

    var obj =    {
        "head": {
            "link": [],
            "vars": ["placeName", "lat", "long"]
        },
        "results": {
            "distinct": false,
            "ordered": true,
            "bindings": [{
                    "placeName": {
                        "type": "literal",
                        "xml:lang": "en",
                        "value": "Ince Minaret Medrese"
                    },
                    "lat": {
                        "type": "typed-literal",
                        "datatype": "http://www.w3.org/2001/XMLSchema#float",
                        "value": "37.8728"
                    },
                    "long": {
                        "type": "typed-literal",
                        "datatype": "http://www.w3.org/2001/XMLSchema#float",
                        "value": "32.49"
                    }
                },
                {
                    "placeName": {
                        "type": "literal",
                        "xml:lang": "en",
                        "value": "Karatay, Konya"
                    },
                    "lat": {
                        "type": "typed-literal",
                        "datatype": "http://www.w3.org/2001/XMLSchema#float",
                        "value": "37.8667"
                    },
                    "long": {
                        "type": "typed-literal",
                        "datatype": "http://www.w3.org/2001/XMLSchema#float",
                        "value": "32.5289"
                    }
                }
            ]
        }
    }
;
    var myJSON = JSON.parse(obj);
    document.getElementById("demo").innerHTML = myJSON.results.bindings[0].placeName.value;

    </script>

but I don't get any results, how am I supposed to extract data from nested objects?

Respectively:

obj['results']['bindings'][0]['placeName']['value']

obj['results']['bindings'][1]['placeName']['value']

obj['results']['bindings'][1]['lat']['value']

obj['results']['bindings'][1]['long']['value']

obj['results']['bindings'][0]['lat']['value']

obj['results']['bindings'][0]['long']['value']

You can also use dot notation (eg obj.results.bindings[1].long.value), which is recommended if you know the indexes before you access them.

FYI: You don't need to parse the object because it is already JSON.

 var obj = { "head": { "link": [], "vars": ["placeName", "lat", "long"] }, "results": { "distinct": false, "ordered": true, "bindings": [{ "placeName": { "type": "literal", "xml:lang": "en", "value": "Ince Minaret Medrese" }, "lat": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "37.8728" }, "long": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "32.49" } }, { "placeName": { "type": "literal", "xml:lang": "en", "value": "Karatay, Konya" }, "lat": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "37.8667" }, "long": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "32.5289" } } ] } }; console.log(obj['results']['bindings'][0]['placeName']['value'], obj['results']['bindings'][1]['placeName']['value'], obj['results']['bindings'][1]['lat']['value'], obj['results']['bindings'][1]['long']['value'], obj['results']['bindings'][0]['lat']['value'], obj['results']['bindings'][0]['long']['value']); 

Your code is correct actually, just remove JSON.parse() because the parameter you are passing to the parse method is not a json string, its an actual object
So do it the following way.
var myJSON = obj; And it will start working.

You can using filter

var result = data.results.bindings.filter(function( obj ) {
    return obj.placeName.value == "Ince Minaret Medrese";
});

See the MDN Docs on Array.prototype.filter()

 var data = { "head": { "link": [], "vars": ["placeName", "lat", "long"] }, "results": { "distinct": false, "ordered": true, "bindings": [{ "placeName": { "type": "literal", "xml:lang": "en", "value": "Ince Minaret Medrese" }, "lat": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "37.8728" }, "long": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "32.49" } }, { "placeName": { "type": "literal", "xml:lang": "en", "value": "Karatay, Konya" }, "lat": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "37.8667" }, "long": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#float", "value": "32.5289" } } ] } }; var result = data.results.bindings.filter(function( obj ) { return obj.placeName.value == "Ince Minaret Medrese"; }); console.log(result) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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