【问题标题】:Parsing json containing array of array gives 'undefined'解析包含数组数组的 json 给出“未定义”
【发布时间】:2019-09-04 05:33:07
【问题描述】:

我正在尝试以 JSON 格式访问数组的数组中的元素,但我收到了undefined

错误 - 未捕获的类型错误:无法读取第 1 行未定义的属性“lsi_short_name”。 9 $("#jsondata").append(

代码如下:

var data = '{"response":[[{"1":{"type":"p","lsi_short_name":"A","entities":["term_Quantity"]}}, {"2":{"type":"p","lsi_short_name":"B","entities":["term_Quantity"]}}, {"4":{"type":"d","lsi_short_name":"D","entities":["term_Quantity"]}}, {"5":{"type":"p","lsi_short_name":"E","entities":["term_Quantity"]}}], [{"1":{"type":"p","lsi_short_name":"A","entities":["term_Quantity"]}}, {"2":{"type":"p","lsi_short_name":"B","entities":["term_Quantity"]}}, {"4":{"type":"d","lsi_short_name":"D","entities":["term_Quantity"]}}, {"6":{"type":"p","lsi_short_name":"F","entities":["term_Quantity"]}}]]}';
var obj = JSON.parse(data);

for(i=0;i<obj.response.length;i++) {
    for (j=0; j < obj.response[i].length; j++) {
        $("#jsondata").append("<li onclick=jsonDetails('"+obj.response[i][j][j+1]['lsi_short_name'] +"','"+ obj.response[i][j][j+1]['entities']+"','"+ obj.response[i][j][j+1]['attributes']+"')>"+obj.response[i][j][j+1]['lsi_short_name']+"</li>");  
    }
    $("#jsondata").append("<br>");
}

提前致谢。

【问题讨论】:

  • @Jaromanda 但它显示了除第 2 个数组的 2 个元素以外的所有元素的结果
  • 你需要在开始内部for循环之前捕获元素。
  • 好的……刚刚意识到你这样做了……但是你的循环看起来不对
  • 哦,我明白了,里面有一个带有数字键的 OBJECT,它不是数组
  • 索引 0 有一个键为 1 的对象 - 所以 [j][j+1] 将起作用,类似地,索引 1 有一个键为 2 的对象。但索引 2 有一个键为 4 的对象,并且 3 有 5 ......它们是加号......在结果 [0] ......在结果 [1] 你的对象有键 1,2,4,6 ......同样前两个可以工作,但是最后两个不会

标签: javascript arrays json parsing


【解决方案1】:

您的密钥顺序不是串行的,这就是为什么您不应该使用j+1。因此,您可以先使用Object.keys() 获取确切的密钥,然后访问属性:示例:

var data = '{"response":[[{"1":{"type":"p","lsi_short_name":"A","entities":["term_Quantity"]}}, {"2":{"type":"p","lsi_short_name":"B","entities":["term_Quantity"]}}, {"4":{"type":"d","lsi_short_name":"D","entities":["term_Quantity"]}}, {"5":{"type":"p","lsi_short_name":"E","entities":["term_Quantity"]}}], [{"1":{"type":"p","lsi_short_name":"A","entities":["term_Quantity"]}}, {"2":{"type":"p","lsi_short_name":"B","entities":["term_Quantity"]}}, {"4":{"type":"d","lsi_short_name":"D","entities":["term_Quantity"]}}, {"6":{"type":"p","lsi_short_name":"F","entities":["term_Quantity"]}}]]}';
//var data = '{"response": "[[{"1": {"attributes": [], "entities": [], "lsi_short_name": "a", "type": "process"}}, {"2": {"attributes": ["d"], "entities": ["c"], "lsi_short_name": "b", "type": "process"}}], [{"1": {"attributes": [], "entities": [], "lsi_short_name": "a", "type": "process"}},{"6": {"attributes": [], "entities": [], "lsi_short_name": "f", "type": "process"}}]]"}';

var obj = JSON.parse(data);
for(var i = 0; i < obj.response.length; i++) {
    for (var j = 0; j < obj.response[i].length; j++) {
        var key = Object.keys(obj.response[i][j])[0];
        $("#jsondata").append("<li onclick=jsonDetails('" + obj.response[i][j][key]['lsi_short_name'] +"','"+ obj.response[i][j][key]['entities']+"','"+ obj.response[i][j][key]['attributes']+"')>"+obj.response[i][j][key]['lsi_short_name']+"</li>");
    }
    $("#jsondata").append("<br>");
}

【讨论】:

    【解决方案2】:

    您正试图访问超出数组长度的对象。您也没有任何名为 attributes 的属性。

    你可以先从对象中获取值,然后像下面这样使用索引:

    var data = '{"response":[[{"1":{"type":"p","lsi_short_name":"A","entities":["term_Quantity"]}}, {"2":{"type":"p","lsi_short_name":"B","entities":["term_Quantity"]}}, {"4":{"type":"d","lsi_short_name":"D","entities":["term_Quantity"]}}, {"5":{"type":"p","lsi_short_name":"E","entities":["term_Quantity"]}}], [{"1":{"type":"p","lsi_short_name":"A","entities":["term_Quantity"]}}, {"2":{"type":"p","lsi_short_name":"B","entities":["term_Quantity"]}}, {"4":{"type":"d","lsi_short_name":"D","entities":["term_Quantity"]}}, {"6":{"type":"p","lsi_short_name":"F","entities":["term_Quantity"]}}]]}';
    
    var obj = JSON.parse(data);
    for(let i=0;i<obj.response.length;i++){  
      for (let j=0; j < obj.response[i].length; j++) {
      let o = Object.values(obj.response[i][j])[0];
      $("#jsondata").append("<li onclick=jsonDetails('"+o['lsi_short_name'] +"','"+ o['entities'][0]+"')>"+o['lsi_short_name']+"</li>");
      }
      $("#jsondata").append("<br>");
    }
    
    function jsonDetails(sn, en){
      console.log(sn + '::' + en)
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="jsondata"></div>

    【讨论】:

      【解决方案3】:

      您的 for 循环应如下所示:

      var obj = JSON.parse(data);
      for(i=0;i<obj.response.length;i++){
          var x = obj.response[i];
          for (j=0; j < x.length; j++) {    
             //Assuming the keys are serial else you need to get all the keys using Object.keys method.
             var keyName = (j+1).toString();
             //Access various prop of your json like this -->    x[j][keyName]["lsi_short_name"]
          }
      }
      

      【讨论】:

      • 在 x.length 处出现错误 - 未捕获的类型错误:无法读取未定义的属性“长度”
      • 尝试使用 var x = obj.response[i]; (更新了我上面的代码)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多