简体   繁体   English

如何从json嵌套对象中提取数据?

[英]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 我有下面的JSON文件,我想提取placeName的值,即“ Ince Minaret Medrese”和“ Karatay,Konya”,以及lat和long的值分别为37.8728和32.49,对于“ Karatay,Konya”,lat的值为37.8667和long的值是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'] [0] ['placeName'] ['value']

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

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

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

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

obj['results']['bindings'][0]['long']['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. 您也可以使用点表示法(例如obj.results.bindings [1] .long.value),如果在访问索引之前先知道索引,则建议使用该符号。

FYI: You don't need to parse the object because it is already JSON. 仅供参考:您不需要解析对象,因为它已经是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 您的代码实际上是正确的,只需删除JSON.parse()因为您传递给parse方法的参数不是json字符串,而是一个实际对象
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() 请参阅Array.prototype.filter()上MDN文档

 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) 

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

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