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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵