简体   繁体   English

如何读取复杂的嵌套json数据

[英]How to read a complex and nested json data

My question is, how can you read a specefic form of json data using javascript, for example if i had this one, it's seems to me a bet difficult, so can you help me out. 我的问题是,如何使用javascript读取特定形式的json数据,例如,如果我有此格式,对我来说似乎很困难,所以可以帮助我。

{
  "jQRReponse": [
    [
      {
        "sujet": "RHONE ALPES",
        "verbe": "est_le_nom_de_la_region",
        "complement": {
          "sujet": "82",
          "verbe": "est_la_region_du_dept",
          "complement": {
            "sujet": "01",
            "verbe": "est_le_numero_du_dept",
            "complement": {
              "sujet": "Ain",
              "verbe": "contient_les_resultats_de_depAnn",
              "complement": {
                "sujet": "Ain2014",
                "verbe": "Pop_results_Ens_Total",
                "complement": "626794"
              }
            }
          }
        }
      }
    ],
    [
      {
        "sujet": "RHONE-ALPES",
        "verbe": "est_le_nom_de_la_region",
        "complement": {
          "sujet": "82",
          "verbe": "est_la_region_du_dept",
          "complement": {
            "sujet": "01",
            "verbe": "est_le_numero_du_dept",
            "complement": {
              "sujet": "Ain",
              "verbe": "contient_les_resultats_de_depAnn",
              "complement": {
                "sujet": "Ain2014",
                "verbe": "Pop_results_Ens_Total",
                "complement": "626794"
              }
            }
          }
        }
      }
    ]
  ]
}

Say if I had this form: 假设我有这个表格:

data : [{
toto:5,
mama:10
},
{
toto:99,
mama:10
},
{
toto:88,
mama:10
}]

I'm gonna read the toto value at the index i like this : data[i].toto . 我将在这样的索引上读取toto值: data[i].toto

So how can I do it for the first one. 那么我该如何做第一个。

Thank you 谢谢

You can access the specific part of this nested json object if you know the element, depth and the key. 如果知道元素,深度和键,则可以访问此嵌套json对象的特定部分。

Say if your element is 0, depth is 2 and the key is verbe , then you can do like this: 假设您的元素为0,深度为2,键为verbe ,则可以这样:

nestedObj[0].complement.complement.complement.verbe

If your requirement is to traverse the whole object and find something you need, you can use a recursive function for each element. 如果您需要遍历整个对象并找到所需的内容,则可以对每个元素使用递归函数。

function traverse_it(obj){

    for(var prop in obj){

        console.log(obj[prop]);            //Or whatever you want to do with this level

        if(typeof obj[prop]=='object'){
           traverse_it(obj[prop[i]]);      //Function goes to the next level here

        }
    }
}

traverse_it(nestedObj);

Here is lot of insight 这里有很多见识

You could iterate over all data and get the ressult. 您可以遍历所有数据并获得补偿。

The access is in the form like 访问的形式如下

data.jQRReponse[0][0].complement.complement.sujet
// returns "01"

 function read(o) { Object.keys(o).forEach(function (k) { if (o[k] !== null && typeof o[k] === 'object') { console.log(k + ' -->'); read(o[k]); return; } console.log(k + ': ' + o[k]); }); } var data = { "jQRReponse": [[{ "sujet": "RHONE ALPES", "verbe": "est_le_nom_de_la_region", "complement": { "sujet": "82", "verbe": "est_la_region_du_dept", "complement": { "sujet": "01", "verbe": "est_le_numero_du_dept", "complement": { "sujet": "Ain", "verbe": "contient_les_resultats_de_depAnn", "complement": { "sujet": "Ain2014", "verbe": "Pop_results_Ens_Total", "complement": "626794" } } } } }], [{ "sujet": "RHONE-ALPES", "verbe": "est_le_nom_de_la_region", "complement": { "sujet": "82", "verbe": "est_la_region_du_dept", "complement": { "sujet": "01", "verbe": "est_le_numero_du_dept", "complement": { "sujet": "Ain", "verbe": "contient_les_resultats_de_depAnn", "complement": { "sujet": "Ain2014", "verbe": "Pop_results_Ens_Total", "complement": "626794" } } } } }]] }; read(data, []); 

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

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