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

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 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误