0

I have a jquery json object like:

{"meta":{"limit":20,"next":null,"offset":0,"previous":null,"total_count":2},
"objects":[
{"body":"Body 1","date":"2013-01-15},
{"body":"Body 2","date":"2013-02-25}
 ]}

I would like to display looped data:

body 1, date
body 2, date
asked Mar 4, 2013 at 13:33
4
  • could you maybe provide the HTML that you would like to display this information? have you thought of using a templating plugin? Commented Mar 4, 2013 at 13:35
  • What the hell is a jQuery-JSON object? :P Commented Mar 4, 2013 at 13:37
  • 2
    Also your json is invalid you need to close your date with " otherwise nothing will work ie "date":"2013年01月15日" Commented Mar 4, 2013 at 13:38
  • You can try like this objects[0]["body"] hope it will work Commented Mar 4, 2013 at 13:41

5 Answers 5

2

try this

var data= "yourjson";
$.each(data.objects,function(i,v){ 
 alert(v.body);
 alert(v.date);
});

had to correct some of the issues in the json.. like missing "... so please check the fiddle.

fiddle here

answered Mar 4, 2013 at 13:39
Sign up to request clarification or add additional context in comments.

Comments

2

First of all, your JSON is invalid. You lack parentheses after date. This is a valid JSON:

var json = {"meta":{"limit":20,"next":null,"offset":0,"previous":null,"total_count":2},
"objects":[
{"body":"Body 1","date":"2013-01-15"},
{"body":"Body 2","date":"2013-02-25"}
]};

Please note the " sign after '2013-01-15' and after '2013-02-25'.

You can display 'body' and 'date' using JQuery's .each():

$.each(json.objects, function (index, obj) {
 console.log(obj.body + " " + obj.date);
});
answered Mar 4, 2013 at 13:40

Comments

1

You need to fix your json first as it is invalid as your not closing your date with "

{
 "meta": {
 "limit": 20,
 "next": null,
 "offset": 0,
 "previous": null,
 "total_count": 2
 },
 "objects": [
 {
 "body": "Body 1",
 "date": "2013-01-15"
 },
 {
 "body": "Body2",
 "date": "2013-02-25"
 }
 ]
}

Then something like this should work

 $.each(data.objects[0],function(i,v){ 
 alert(v.body+" - "+v.date);
 } 
answered Mar 4, 2013 at 13:41

Comments

1
var data = {"meta":{"limit":20,"next":null,"offset":0,"previous":null,"total_count":2},
 "objects":[
 {"body":"Body 1","date":"2013-01-15"},
 {"body":"Body 2","date":"2013-02-25"}
 ]};
for(var i = 0; i < data.objects.length; i++){
 alert(data.objects[i].body);
 alert(data.objects[i].date);
}
answered Mar 4, 2013 at 13:43

Comments

0

JSON objects are like key-value pairs.

Suppose this whole JSON object is in variable data then to read the content, do the following.

meta is an object containing different data, so to read limit, next and others you have to use data.meta.limit and so on.

Now objects is array type in which there are different objects.

So to read body, execute data.objects[0].body and so on.

answered Mar 4, 2013 at 13:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.