-1

有効な json オブジェクトがありますが、jquery/javascript を使用してこれからメモリを抽出しようとすると、常に「未定義」として返されます。

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..?

hotelImageId以下のこのjsonオブジェクトから単一のものを抽出する方法を知っている人はいますか?

nb: JSON.stringify を 2 回実行すると、[オブジェクト オブジェクト] と表示されるためです。


{
   "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
            }
         ]
      }
   }
}
4

4 に答える 4

1

あなたが書く

var json = (the string below).

したがって、変数jsonstring型です。

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

この意味

JSON.stringify(json)

文字列を JSON エンコード文字列にシリアライズします。そう'"a"'なる"\"a\""

JSON.stringify(JSON.stringify(json))

うーん、stringify が好きなようですね... さらにエンコーディング...

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

なぜあなたはしないJSON.parse()のですか?また、二重にエンコードされた文字列は返されません。

于 2012-11-30T04:17:32.527 に答える
0

オブジェクトをループする必要があります

そして、できる項目ごとに

Foreach (オブジェクト、アイテム) アラート (item.HotelInformationResponse);

完璧なコードをチェックしてください、私はあなたにsudoのアイデアを与えました.

于 2012-11-30T04:11:21.640 に答える
0

リクエストに応じて、単一の hotelImageId を抽出する方法を示す jsfiddle の例を次に示します。

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

例のコード スニペットを次に示します。

    var hotelImage = jsonObj.HotelInformationResponse.HotelImages.HotelImage[0],
    singleImageId = hotelImage.hotelImageId;
于 2012-11-30T04:23:46.030 に答える
0

それは正常に動作し、それを証明するための jsFiddle があります: http://jsfiddle.net/jesus_tesh/xL4Gw/

私はそれを思いつき、カラーボックスを使用するようにしました.サムネイルをクリックすると、フルサイズの画像が表示されます.

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; }

</p>

于 2012-11-30T04:34:08.147 に答える