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

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 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)