首页 文章

无法使用 JSON 与 jQuery 进行解析

提问于
浏览
0

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"
        }]
    }

例外

uncaught typeerror 无法读取 undefined json 的属性'length'

**** 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

1 回答

  • 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>
    

相关问题