Parsing JSONP file with JavaScript -


how access marie-antoinette.json object in json file? title element of object can't seem output. here javascript code outputs object cant seem access objects elements.

 $.ajax(     {         type: 'get',         url: 'http://localhost:5984/movies/efadd5913f5cfd254b2861efd4001cb7',         //contenttype: "application/json; charset=utf-8",         datatype: "jsonp",         jsonpcallback: 'callback',         //async: false,         success: function(r)         {             alert("ok");             $.each(r, function(index, value){ // iterating on each object                 console.log(index);                 if(index == "_attachments")                 {                     console.log(value); //how output title ("marie-antoinette.json") , other stuff in object?                 }             });         }     }); 

here file. elements access in "_attachments" element of object.

{     "_id": "efadd5913f5cfd254b2861efd4001cb7",     "_rev": "6-417588bbff9aa74726b11440a86a8532",     "_attachments": {         "marie-antoinette.json": {             "content_type": "application/json",             "revpos": 2,             "digest": "md5-io/pxakfp/4r8ntjqkwmdg==",             "length": 761,             "stub": true         }     } } 

i think throwing me off is object inside _attachment section.

the marie-antoinette.json object inside _attachments object, because contains . can't accessed using dot-notation. you'll have use array-like notation, passing key string so:

success: function (response) {     console.log(response._attachments['marie-antoinette.json']); } 

if have multiple "attachments", can access them in loop this:

success: function (response) {     $.each(response._attachments, function (i, attachment) {         console.log(attachment);     }); } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -