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

Exception

uncaught typeerror cannot read property 'length' of undefined 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>

Note: jQuery used: jquery-1.9.1.min.js

2
  • 1
    valid JSON according to jsonlint.com, please post the JavaScript code you are using to parse Commented Nov 13, 2013 at 5:56
  • <script> $.getJSON("url", { tags : "dogs", tagmode : "any", format : "json" }, function(data) { console.log("length" + data.length); $.each(data.items, function(i, item) { }); }); </script> Commented Nov 13, 2013 at 5:57

1 Answer 1

1

Please made changes in your JS code at success call back. Because data.item is undefined.

There is no item key on that json response.

<script> $.getJSON("url", { tags : "dogs", tagmode : "any", format : "json" }, function(data) { console.log("length" + data.length); $.each(data, function(i, item) { }); }); </script>
Sign up to request clarification or add additional context in comments.

3 Comments

How can i parse and display each "title" from the JSON?
Need to use one more each inside first each. $.each(data, function (outKey, outerObjVal) { $.each(outerObjVal, function (innerKey, innerObjVal) { console.log(innerObjVal.title); }); }); If you got solution, accept my answer
Can you please write one function for above JSON?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.