weixin_33722405 2017-01-20 14:01 采纳率: 0%
浏览 1226

JSON值显示NaN

While retrieving JSON array data using $.each method, I am able to get the array values, but apart from that it's showing 'NaN' values and showing the below error.

"Uncaught TypeError: Cannot use 'in' operator to search for '5' in Hello!"

Seems it's looping through all the objects not only array, if this is the case how can I get only array values and how can I get all the values(including and excluding array values).

one more query does this rawgit works for only method 'GET', because while using method 'POST' throwing error(403 (Forbidden)).

    $.ajax({ 
                method: 'GET', 
                url: 'https://rawgit.com/rajeshdesigner/testgit/master/colorsdata.json',
                dataType: 'json',
                success: function (data) { 
                    $.each(data, function(index, obj){
                        $.each(obj, function(index, element) {
                            $('#show-data').append(element.key + element.value + '<br/>');
                        });             
                    });
                }
           });

JSON:

{
      "items": [
        {
          "key": "First",
          "value": 100
        },{
          "key": "Second",
          "value": 200
        },{
          "key": "Last",
          "value": "Mixed"
        }
      ],
      "obj": {
        "number": 1.2345e-6,
        "enabled": true
      },
      "message": "Hello!"
    }
  • 写回答

6条回答 默认 最新

  • weixin_33712987 2017-01-20 14:08
    关注

    It sounds like you only want to iterate data.items but you're iterating over everything. Try this:

    $.each(data.items, function(index, obj) {
      $('#show-data').append(obj.key + obj.value + '<br/>');
    });
    

    https://jsfiddle.net/rrsku4Lq/

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器