【问题标题】:Not able to parse using JSON with jQuery无法使用带有 jQ​​uery 的 JSON 进行解析
【发布时间】:2013-11-13 05:52:46
【问题描述】:

JSON

 {
        "dogs": [{
            "bid": "qiDfDRun",
            "title": "What if??",
            "dogType": "image",
            "dogImageUrl": "dog-027",
            "closedBottleImage": "",
            "openBottleImage": "",
            "dateCreated": "20 hours ago",
            "distance": 10136,
            "catUrl": "cat-024.gif",
            "message": "",
            "imageurl": "",
            "likes": {
                "likeCount": 2
            },
            "locationsCount": 6,
            "u_index": 0,
            "username": "bestill",
            "realname": "Be Still",
            "aid": -1,
            "socialcamimg": "",
            "soundcloud_url": "",
            "vidUrl": "",
            "vidfrom": "Youtube",
            "vimeoimg": "",
            "imageName": "qiDfDRun_large.jpg",
            "redogd": {
                "uid": 0
            },
            "width": "900",
            "height": "805",
            "small_image": "qiDfDRun_large_small.jpg"
        },
        {
            "bid": "x7nfI2dr",
            "title": "~ Autumn ~",
            "dogType": "AudioUrl",
            "dogImageUrl": "dog-001",
            "closedBottleImage": "",
            "openBottleImage": "",
            "dateCreated": "20 hours ago",
            "distance": 11410,
            "catUrl": "cat-034.gif",
            "message": "",
            "imageurl": "",
            "likes": {
                "likeCount": 1
            },
            "locationsCount": 9,
            "u_index": 1,
            "username": "Donna_Powell",
            "realname": "Donna Powell",
            "aid": -1,
            "socialcamimg": "",
            "soundcloud_url": "https:\/\/api.soundcloud.com\/tracks\/3024251",
            "vidUrl": "",
            "vidfrom": "Youtube",
            "vimeoimg": "",
            "imageName": "x7nfI2dr_large.gif",
            "redogd": {
                "uid": 0
            },
            "width": "232",
            "height": "302",
            "audiourl_url": "",
            "audiofrom": "Soundcloud",
            "small_image": "x7nfI2dr_large.gif"
        },
        {
            "bid": "4GGLgvrt",
            "title": "Going for the Treat!",
            "dogType": "image",
            "dogImageUrl": "dog-020",
            "closedBottleImage": "",
            "openBottleImage": "",
            "dateCreated": "20 hours ago",
            "distance": 9757,
            "catUrl": "cat-014.gif",
            "message": "",
            "imageurl": "",
            "likes": {
                "likeCount": 1
            },
            "locationsCount": 6,
            "u_index": 2,
            "username": "HereKitty",
            "realname": "Cats & Kittens",
            "aid": 149,
            "socialcamimg": "",
            "soundcloud_url": "",
            "vidUrl": "",
            "vidfrom": "Youtube",
            "vimeoimg": "",
            "imageName": "4GGLgvrt_large.gif",
            "redogd": {
                "uid": 0
            },
            "width": "400",
            "height": "300",
            "small_image": "4GGLgvrt_large.gif"
        },
        {
            "bid": "MapkxKPF",
            "title": "Me on a walk today!",
            "dogType": "image",
            "dogImageUrl": "dog-020",
            "closedBottleImage": "",
            "openBottleImage": "",
            "dateCreated": "2 days ago",
            "distance": 11788,
            "catUrl": "cat-096.gif",
            "message": "",
            "imageurl": "",
            "likes": {
                "likeCount": 3
            },
            "locationsCount": 13,
            "u_index": 3,
            "username": "TheBrunsonChronicles",
            "realname": "The Brunson Chronicles",
            "aid": -1,
            "socialcamimg": "",
            "soundcloud_url": "",
            "vidUrl": "",
            "vidfrom": "Youtube",
            "vimeoimg": "",
            "imageName": "MapkxKPF_large.jpg",
            "redogd": {
                "uid": 0
            },
            "width": "2448",
            "height": "3264",
            "small_image": "MapkxKPF_large_small.jpg"
        }],
        "users": [{
            "avatarSm": "b37176744d8a40f2cb07c5b21e758205_s.jpg",
            "realname": "Be Still",
            "state": "United_States_of_America",
            "uid": 742,
            "name": "bestill"
        },
        {
            "avatarSm": "user_73_small.jpg",
            "realname": "Donna Powell",
            "state": "United_States_of_America",
            "uid": 73,
            "name": "Donna_Powell"
        },
        {
            "avatarSm": "4b208efafa2a137db2835a61f3d81749_s.jpg",
            "realname": "Cats & Kittens",
            "state": "United_States_of_America",
            "uid": 126,
            "name": "HereKitty"
        },
        {
            "avatarSm": "230bd063de26a1c05fb63fff848cb0bc_s.jpeg",
            "realname": "The Brunson Chronicles",
            "state": "United_States_of_America",
            "uid": 741,
            "name": "TheBrunsonChronicles"
        }]
    }

例外

未捕获的类型错误无法读取未定义 json 的属性“长度”

JS

<script> 
    $.getJSON("url", {
        tags: "dogs",
        tagmode: "any",
        format: "json"
    }, function (data) {
        console.log("length" + data.length);
        $.each(data.items, function (i, item) {});
    }); 
</script>

注意:使用的jQuery:jquery-1.9.1.min.js

【问题讨论】:

  • 根据jsonlint.com的有效JSON,请贴出你用来解析的JavaScript代码

标签: jquery json cordova


【解决方案1】:

请在成功回调时更改您的 JS 代码。因为data.item 是未定义的。

该 json 响应中没有 item 键。

<script> $.getJSON("url", { tags : "dogs", tagmode : "any", format : "json" }, function(data) { console.log("length" + data.length); $.each(data, function(i, item) { }); }); </script>

【讨论】:

  • 如何解析和显示 JSON 中的每个“标题”?
  • 需要先在每个里面再使用一个。 $.each(data, function (outKey, outerObjVal) { $.each(outerObjVal, function (innerKey, innerObjVal) { console.log(innerObjVal.title); }); });如果你有解决办法,接​​受我的回答
  • 能否为上面的 JSON 写一个函数?
猜你喜欢
  • 2012-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
相关资源
最近更新 更多