JQuery Parsen von JSON-Objekt - Funktioniert Nicht?
Ich habe ein gültiges json-Objekt, aber wenn ich versuche, zu extrahieren, Arbeitsspeicher dieses mit jquery/javascript es kommt immer wieder als 'nicht definiert'.
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..?
Weiß jemand, wie ich die extrahieren kann eine einzelne hotelImageId
aus diesem json-Objekt unten?
nb: JSON.stringify zweimal, weil wenn ich es einmal tun, es sagt noch [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
}
]
}
}
}
- Warum auf der Erde Sie konvertieren von JSON-Objekt in einen string (zweimal) und wieder zurück nach JSON?
- "Warnungen das Objekt korrekt" Sind Sie sicher? Es sollten aufmerksam
[object Object]
wenn es wirklich ein Objekt ist. Wenn Sie die tatsächlichen Eigenschaften des Objekts, haben Sie immer noch ein string. edit: Nur Lesen "JSON.stringify zweimal, weil wenn ich es einmal tun, es sagt noch [Object Object]". Das ist die richtige Ausgabe. Wennjson
ist ein string, nurJSON.parse(json)
. Wenn es tatsächlich ein Objekt, das Sie nicht haben, etwas zu tun. - "nb: JSON.stringify zweimal, weil wenn ich es einmal tun, es sagt noch [Object Object]": Das passiert nicht, wenn ich versuche es in Chrome 23. Dort werden die ersten JSON.stringify gibt einen string zurück, kein Objekt.
- möglich, Duplikat der ich habe eine verschachtelte Datenstruktur / JSON, wie sich der Zugang zu einem bestimmten Wert?
- denn wenn ich es einmal tun: ie: var obj = $.parseJSON(JSON.stringify(json)); und ich weiß alert(obj), es sagt noch [Object Object]...
- Entschuldigt also, wenn ich es im Objekt - ... wie bekomme ich eine Liste der Bilder und Blättern Sie durch die Liste der Alarmierung jedes Bild. wenn ich alert(obj.HotelInformationResponse) geht es wieder mit undefinierten und dann obj.HotelInformationResponse.HotelImage kommt zurück mit undefined
- "ich weiß alert(obj), es sagt noch [Object Object]", die mir sagt, dass
json
ist bereits ein Objekt und du musst nicht konvertieren, es überhaupt nicht. Die Standard-Zeichenfolge respresentation eines Objekts ist[object Object]
, so dass, wenn Siealert
ein Objekt, das ist zu erwarten. Verwendenconsole.log
wenn Sie wollen, um tatsächlich zu inspizieren die variable. Um zu erfahren, wie man auf einen bestimmten Wert, einen Blick auf den link, den ich gepostet in meinem vorherigen Kommentar. - versuchen
json.HotelInformationResponse.HotelImages.HotelImage[0].hotelImageId
- Josua : können Sie mir Ihren code, wie würden Sie gehen über jeder "hotelImageId" in dieser Liste....?
- Ich sage es noch einmal, einen Blick auf den link, den ich weiter oben geschrieben: stackoverflow.com/questions/11922383/.... Habe ich eine detaillierte (so denke ich) Erklärung, wie man auf verschachtelte Datenstrukturen in JS.
- nie Verstand ich es herausgefunden - danke Jungs! wer sagt, das Objekt ist bereits im Objekt habe meinen Kopf um ihn herum:)
Du musst angemeldet sein, um einen Kommentar abzugeben.
Schreiben Sie
So variabel
json
iststring
geben.Bedeutet dies,
String serialisieren zu JSON-kodierten string. So
'"a"'
geworden"\"a\""
Hmm, es scheint, Sie lieben stringify... mehr Codierung...
Warum Sie nicht
JSON.parse()
. Und doppelt kodierter string wird nicht zu dir zurück.Haben Sie, um eine Schleife Ihr Objekt
Dann für jedes Element, das Sie tun können,
Foreach(Objekt,Element)
alert(Element.HotelInformationResponse);
Überprüfen Sie die perfekten code , die ich dir gegeben habe sudo Idee.
HotelInformationResponse
scheint zu sein, eine Eigenschaft, die der top-level-Objekt, also wennobj
ist eigentlich ein Objekt,obj.HotelInformationResponse
sollte tun gut.Hier ein jsfiddle-Beispiel, das zeigt, wie Sie zu extrahieren eine einzelne hotelImageId, wie pro Ihre Anforderung:
http://jsfiddle.net/nickadeemus2002/9knzN/
hier ist ein Codeausschnitt aus dem Beispiel:
Es funktioniert gut, und hier ist ein jsFiddle, um es zu beweisen: http://jsfiddle.net/jesus_tesh/xL4Gw/
Habe ich sogar davon geträumt, es und es verwenden, eine colorbox - klicken Sie auf das Miniaturbild und es zeige das Bild in voller Größe.
JS
CSS