0

I am trying to look through an object, but based on the structure I do not know how to get to the data.

Object:

{
"177":{
     "text":"test text",
     "user_name":"Admin",
     "date":"1385494358",
     "docs":{
        "document_name": [
            "marketing_service",
            "maintenance_service",
            "development_service"],
        "document_type":[
            "png",
            "png",
            "png"]
       }
},
"174":{
     "text":"Some more images",
     "user_name":"Admin",
     "date":"1385493618",
     "docs":{
        "document_name": [
            "marketing_service16",
            "maintenance_service53"],
     "document_type":[
            "png","png"]
      }
}
}

The loop I am attempting in jQuery

var obj = $.parseJSON(data);
$(obj).each(function(index, note) {
    console.log(note.text + note.user_name + note.date_created);
});

It is returning undefined. What am I doing wrong?

3
  • What is data is it a string? Commented Nov 26, 2013 at 21:08
  • data is the object displayed in the above code Commented Nov 26, 2013 at 21:09
  • Please show exactly how you declare and set data. If data is already an object you don't want to use $.parseJSON(). Commented Nov 26, 2013 at 21:09

4 Answers 4

1

Try like this

var obj = $.parseJSON(data);
for(var n in obj){
 var note = obj[n]
 console.log(note.text + note.user_name + note.date_created);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Basically what I said :-P
0

To loop through a json object just use a for loop.

The way you are doing it will return errors because it is trying to select that json object from the page which does not exist.

Sample code:

var obj = $.parseJSON(data); //assuming `data` is a string
for(index in obj) {
    var note = obj[index];
    console.log(note.text + note.user_name + note.date_created);
};

9 Comments

You should be very careful using for in without checking the object has the property first.
What do you mean @DavidBarker ?
"it is trying to select that json object from the page" - No it isn't.
@qwertnl obj.prototype is a property of the object, as is obj.length. When using for in loops you need to check if (object.hasOwnProperty(property)) before attempting to gather data from it.
Using .hasOwnProperty should only be used when it makes sense to use it. It doesn't make sense to use it as a broad guard on every single loop of every single object.
|
0

its a little better to do it like this:

$.each(obj, function ( index, note ) {
    console.log( note.text + note.user_name + note.date_created );
});

this should give you what you need

Comments

0

Working Demo: http://jsfiddle.net/gZ7pd/ or for document http://jsfiddle.net/NGqfB/

Issue is parseJson on the object which is Json.

in the demo above the var obj = $.parseJSON(data); returns null, if I use data it will work.

Hope this helps ;)

Code

var data = {
"177":{
     "text":"test text",
     "user_name":"Admin",
     "date":"1385494358",
     "docs":{
        "document_name": [
            "marketing_service",
            "maintenance_service",
            "development_service"],
        "document_type":[
            "png",
            "png",
            "png"]
       }
},
"174":{
     "text":"Some more images",
     "user_name":"Admin",
     "date":"1385493618",
     "docs":{
        "document_name": [
            "marketing_service16",
            "maintenance_service53"],
     "document_type":[
            "png","png"]
      }
}
};

var obj = $.parseJSON(data);
alert(obj)

$.each(data,function(index, note) {

    alert(note.text + note.user_name + note.date_created);
});

2 Comments

@Downvoter care to reply why a downvote?? :) or is it just personal choice to downvote? not constructive!
@kkemple Thanks bruvoo :) Its very sad some people downvote without any reason! Cheeerios yo! +1 to you!

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.