0

Iam newbie to jquery. I need to parse an JSON. I tried with $each statement but stuck with [object,object]looping. here is the code which i used for parsing the JSON. Help me out of this.

var myjson='[{"isTruncated": "false","nextMarker": "null","marker": "null","prefix":   "Mymedia/mysys/","contents": [{"deviceInfo": "null","lastModified": "Thu Dec 26 16:36:42 IST 2013","etag": "d41d8cd98f00b204e9800998ecf8427e","key":"Mymedia/mysys/audio_$folder$","size": "0"},{"deviceInfo": null,"lastModified": "Thu Dec 26 16:36:11 IST 2013","etag": "d41d8cd98f00b204e9800998ecf8427e","key": "Mymedia/mysys/doc_$folder$","size": "0"},{ "deviceInfo": null,"lastModified": "Thu Dec 26 16:36:20 IST 2013", "etag": "d41d8cd98f00b204e9800998ecf8427e","key": "Mymedia/mysys/imge_$folder$","size": "0"},{"deviceInfo": null,"lastModified": "Thu Dec 26 16:36:56 IST 2013","etag": "d41d8cd98f00b204e9800998ecf8427e","key":"Mymedia/mysys/others_$folder$","size": "0"},{"deviceInfo": null,"lastModified": "Thu Dec 26 16:36:32 IST 2013","etag": "d41d8cd98f00b204e9800998ecf8427e","key": "Mymedia/mysys/video_$folder$","size": "0"}],"name": "name", "statusCode": "200","statusMessage": "Success","error": null}]';

 var dataobj = $.parseJSON(JSON.stringify(myjson));

             $.each(dataobj, function (key, val) {
                 alert(key + val);
                 if (key == "contents") {
                     $.each(val, function (mykey, values) { alert(mykey + values) });
                     $.each(values, function (key, pairs) {alert(pairs) });

                 }

             });

Iam unable to loop through JSON object(contents) and to get the items inside it. I need to get key inside contents object.Point me where i went wrong.

shanthi_karthika
  • 915
  • 2
  • 8
  • 22
  • your have already json string so you don't need to stringify it again. Just parse string like this. var dataobj = $.parseJSON(myjson); and then try to loop object – Sohil Desai Jan 20 '14 at 08:47
  • No need to use JSON.stringify as your json is already in string form so avoid this and use like $.parseJSON(myjson). – Mithlesh Kumar Jan 20 '14 at 10:45

1 Answers1

1

using stack-overflow link mentioned for this question i had solved the issue. I had posted the code below.

 var Listobj = new Array();
 var dataobj = $.parseJSON(JSON.stringify(myjson));

 $.each(dataobj, function (key, val) {
                 alert(key + val);
                 if (key == "contents") {
                     for (var i = 0, l = val.length; i < l; i++) {
                         var obj = val[i];
                         alert(obj.key);
                         Listobj[i] = obj.key;
                     }

                 }

             });
shanthi_karthika
  • 915
  • 2
  • 8
  • 22