-1

I have a valid json object, but when I try to extract memory out of this using jquery/javascript it always comes back as 'undefined'.

var json = (the string below).


var obj = $.parseJSON(JSON.stringify(JSON.stringify(json))); 

alert(obj);       // alerts the object correctly.

alert(obj.HotelInformationResponse);    // undefined..?
alert(obj.HotelInformationResponse.HotelImages); // undefined..?

Does anyone know how i can extract a single hotelImageId from this json object below?

nb: JSON.stringify twice because when i do it once, it still says [Object object]


{
   "HotelInformationResponse":{
      "@hotelId":"106347",
      "customerSessionId":"[xxx]",
      "HotelImages":{
         "@size":"27",
         "HotelImage":[
            {
               "hotelImageId":7092462,
               "name":"",
               "category":1,
               "type":0,
               "caption":"Exterior",
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_57_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_57_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7130885,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_58_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_58_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147393,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_59_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_59_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147394,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_60_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_60_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147395,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_61_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_61_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147396,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_62_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_62_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147397,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_63_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_63_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147398,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_64_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_64_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147399,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_65_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_65_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147400,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_66_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_66_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147401,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_67_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_67_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147402,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_68_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_68_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147403,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_69_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_69_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147404,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_70_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_70_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147405,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_71_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_71_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147406,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_72_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_72_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":7147407,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_73_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_73_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694165,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_30_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_30_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694166,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_31_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_31_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694168,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_33_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_33_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694171,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_36_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_36_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694172,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_37_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_37_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694177,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_42_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_42_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694180,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_45_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_45_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694181,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_46_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_46_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":4694182,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_47_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_47_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            },
            {
               "hotelImageId":6601500,
               "name":"",
               "category":0,
               "type":0,
               "url":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_56_b.jpg",
               "thumbnailUrl":"http://images.travelnow.com/hotels/1000000/10000/5900/5900/5900_56_t.jpg",
               "supplierId":13,
               "width":350,
               "height":350,
               "byteSize":0
            }
         ]
      }
   }
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
Tyler Evans
  • 567
  • 1
  • 8
  • 25
  • 3
    Why on Earth are you converting a JSON object to a string (twice) and back to JSON? – Phil Nov 30 '12 at 04:06
  • 1
    *"alerts the object correctly"* Are you sure? It should alert `[object Object]` if it really is an object. If you see the actual properties of the object, you still have a string. **edit**: Just read *"JSON.stringify twice because when i do it once, it still says [Object object]"*. That's the correct output. If `json` is a string, just do `JSON.parse(json)`. If it is actually an object, you don't have to do anything. – Felix Kling Nov 30 '12 at 04:08
  • "nb: JSON.stringify twice because when i do it once, it still says [Object object]": That doesn't happen when I try it in Chrome 23. There, the first JSON.stringify returns a string, not an object. – Joshua D. Boyd Nov 30 '12 at 04:10
  • possible duplicate of [I have a nested data structure / JSON, how can access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-access-a-specific-value) – Felix Kling Nov 30 '12 at 04:13
  • because when i do it once: ie: var obj = $.parseJSON(JSON.stringify(json)); and i do alert(obj) it still says [Object object]... – Tyler Evans Nov 30 '12 at 04:15
  • Apologies so when i have it in object... how do i get a list of the images and browse through that list alerting each image. when i do alert(obj.HotelInformationResponse) it comes back with undefined and then obj.HotelInformationResponse.HotelImage comes back with undefined – Tyler Evans Nov 30 '12 at 04:17
  • *"i do alert(obj) it still says [Object object]"* That tells me that `json` is already an object and you don't have to convert it at all. The default string respresentation of an object is `[object Object]`, so when you `alert` an object, that is to be expected. Use `console.log` if you want to actually inspect the variable. To learn how to access a specific value, have a look at the link I posted in my previous comment. – Felix Kling Nov 30 '12 at 04:17
  • try `json.HotelInformationResponse.HotelImages.HotelImage[0].hotelImageId` – wirey00 Nov 30 '12 at 04:21
  • Joshua : can you send me your code of how you would go through each "hotelImageId" in this list....? – Tyler Evans Nov 30 '12 at 04:21
  • @Tyler: I will say it again, have a look at the link I posted earlier: http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-access-a-specific-value. I provided a detailed (so I think) explanation of how to access nested data structures in JS. – Felix Kling Nov 30 '12 at 04:23
  • never mind i figured it out - thanks guys! whoever said the object is already in object got my head around it:) – Tyler Evans Nov 30 '12 at 04:25

4 Answers4

1

You write

var json = (the string below).

So variable json is string type.

var obj = $.parseJSON(JSON.stringify(JSON.stringify(json))); 

This mean

JSON.stringify(json)

Serialize string to JSON encoded string. So '"a"' become "\"a\""

JSON.stringify(JSON.stringify(json))

Hmm, it seems you love stringify... more encoding...

$.parseJSON(JSON.stringify(JSON.stringify(json))

Why you don't JSON.parse(). And double encoded string won't back to you.

mattn
  • 7,571
  • 30
  • 54
0

You will have to loop your object

And then for each item you can do

Foreach(object,item) alert(item.HotelInformationResponse);

Check the perfect code , i gave you sudo idea.

atul
  • 1
  • 1
    `HotelInformationResponse` seems to be a property of the of top level object, so if `obj` is actually an object, `obj.HotelInformationResponse` should do just fine. – Felix Kling Nov 30 '12 at 04:12
0

Here's a jsfiddle example that shows you how to extract a single hotelImageId, as per your request:

http://jsfiddle.net/nickadeemus2002/9knzN/

here is a code snippet from the example:

    var hotelImage = jsonObj.HotelInformationResponse.HotelImages.HotelImage[0],
    singleImageId = hotelImage.hotelImageId;
chrisvillanueva
  • 1,349
  • 8
  • 9
0

It works fine, and here is a jsFiddle to prove it: http://jsfiddle.net/jesus_tesh/xL4Gw/

I even fancied it up and made it use a colorbox - click the thumbnail and it'll show the full-size image.

JS

var images = json.HotelInformationResponse.HotelImages.HotelImage;
$.each(images, function(idx, data) {
    $("#images")
        .append($("<div></div>")
        .append($("<a></a>").prop("href", data.url).addClass("colorbox")
        .append($("<img></img>").prop("src", data.thumbnailUrl)))
        .append($("<span></span>").text(data.caption || data.hotelImageId)));
    console.log("data", data);
});

$(".colorbox").colorbox();

CSS

html { padding: 20px; }
#images div { display: inline-block; width: 90px; height: 100px; }
#images img { display: block; width: 70px; margin: 0 auto; }
#images span { display: block; text-align: center; }

Tim Hobbs
  • 2,017
  • 17
  • 24