Gelöst! Dank euch allen! Es stellte sich heraus, dass ich direkt mit dem Ergebnis von parseString hätte arbeiten sollen. Das und die JSON-Objekte befanden sich in Arrays.

var content = result.ItemLookupResponse.Items[0].Item[0].ASIN;

^ Dies gab genau das zurück, was ich brauchte.

Ich habe Probleme beim Parsen eines JSON-Objekts, um bestimmte Werte an eine Ansicht zu senden, damit Front-End-Benutzer den Inhalt in JSON sehen können. Ich habe versucht, JSON.stringify () und dann JSON.parse () zu verwenden. Dies führte dazu, dass Folgendes zurückgegeben wurde:

{ '$': { xmlns: 'http://webservices.amazon.com/AWSECommerceService/2011-08-01' },
  OperationRequest: 
   [ { RequestId: [Object],
       Arguments: [Object],
       RequestProcessingTime: [Object] } ],
  Items: [ { Request: [Object], Item: [Object] } ] }

Hier ist der ursprüngliche JSON: http://pastebin.com/XDK9Y0gy

Was ich erhalten muss, ist der ASIN-Wert, der unter Items-> Item-> ASIN zu finden ist.

parseString(xml, function (err, result) {
                //parse the JSON here then res.send that (or render it with the json. do rest from view)
                //data = JSON.stringify(result);
                //var jsonObj = JSON.parse(data);
                //console.log(jsonObj);
                //var ITEMASIN = jsonDoc['Items']['Item']['ASIN'];
                var json = JSON.stringify(result);

                //var rawData = '[{"item1":"tag1","a1":"b1"},{"item2":"tag2","a2":"b2"}]';
                var parsed = JSON.parse(json);
                var content = parsed.ItemLookupResponse.Items[0].Item.ASIN;
                //console.log(parsed.ItemLookupResponse.Items.Item[0].ASIN); // logs "b1"

                res.render('artreview', {content: content});
            });

^ Dies gibt eine undefinierte zurück. Jede Hilfe wird geschätzt

3
matthewninja 21 Aug. 2015 im 23:29

5 Antworten

Beste Antwort

Es sollte sein: result.ItemLookupResponse.Items[0].Item[0].ASIN[0]

Da es sich bei den Daten bereits um ein JavaScript-Objekt handelt, müssen Sie sich nicht um JSON.stringify und JSON.parse kümmern.

3
Matthew Herbst 21 Aug. 2015 im 20:46

Es ist object.ItemLookupResponse.Items[0].Item[0].ASIN[0], wenn Sie Ihren ursprünglichen JSON verwenden

http://jsfiddle.net/fkLtkotm/

2
keja 21 Aug. 2015 im 20:42

Wenn dies wirklich JSON ist, sind die meisten Werte tatsächlich Arrays. Daher müssen Sie wie folgt auf sie zugreifen object.ItemLookupResponse.Items[0].Item[0].ASIN[0].

0
Sylver 21 Aug. 2015 im 20:41

Angenommen, Ihr JSON wird in die Variable myJson analysiert:

myJson.ItemLookupResponse.Items[0].Item[0].ASIN

Gibt Ihnen den ASIN-Wert

0
Edoardoo 21 Aug. 2015 im 20:44

Sie müssen ItemLookupResponse.Items[0].Item[0].ASIN[0]; verwenden.

Im Folgenden finden Sie eine Live-Demo, in der die ASIN des ersten Elements abgerufen und angezeigt wird.

Working Live Demo:

var json = '{"ItemLookupResponse":{"$":{"xmlns":"http://webservices.amazon.com/AWSECommerceService/2011-08-01"},"OperationRequest":[{"RequestId":["4c859017-3b62-401d-83d6-2fec101d3084"],"Arguments":[{"Argument":[{"$":{"Name":"AWSAccessKeyId","Value":"*************"}},{"$":{"Name":"AssociateTag","Value":"*******"}},{"$":{"Name":"ItemId","Value":"B00J07A1CQ"}},{"$":{"Name":"Operation","Value":"ItemLookup"}},{"$":{"Name":"ResponseGroup","Value":"Images,ItemAttributes,OfferSummary,EditorialReview"}},{"$":{"Name":"Service","Value":"AWSECommerceService"}},{"$":{"Name":"Timestamp","Value":"2015-08-21T19:51:57Z"}},{"$":{"Name":"Version","Value":"2009-01-06"}},{"$":{"Name":"Signature","Value":"c**************"}}]}],"RequestProcessingTime":["0.0167620000000000"]}],"Items":[{"Request":[{"IsValid":["True"],"ItemLookupRequest":[{"IdType":["ASIN"],"ItemId":["B00J07A1CQ"],"ResponseGroup":["Images","ItemAttributes","OfferSummary","EditorialReview"],"VariationPage":["All"]}]}],"Item":[{"ASIN":["B00J07A1CQ"],"DetailPageURL":["http://www.amazon.com/House-with-Turret/dp/B00J07A1CQ%3FSubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00J07A1CQ"],"ItemLinks":[{"ItemLink":[{"Description":["Technical Details"],"URL":["http://www.amazon.com/House-with-Turret/dp/tech-data/B00J07A1CQ%3FSubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]},{"Description":["Add To Baby Registry"],"URL":["http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB00J07A1CQ%26SubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]},{"Description":["Add To Wedding Registry"],"URL":["http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB00J07A1CQ%26SubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]},{"Description":["Add To Wishlist"],"URL":["http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB00J07A1CQ%26SubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]},{"Description":["Tell A Friend"],"URL":["http://www.amazon.com/gp/pdp/taf/B00J07A1CQ%3FSubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]},{"Description":["All Customer Reviews"],"URL":["http://www.amazon.com/review/product/B00J07A1CQ%3FSubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]},{"Description":["All Offers"],"URL":["http://www.amazon.com/gp/offer-listing/B00J07A1CQ%3FSubscriptionId%3DAKIAIH6WQH3RFEQC6GNQ%26tag%3Dmercartto-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00J07A1CQ"]}]}],"SmallImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"57","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"121","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"377","$":{"Units":"pixels"}}]}],"ImageSets":[{"ImageSet":[{"$":{"Category":"variant"},"SwatchImage":[{"URL":["http://ecx.images-amazon.com/images/I/51Z632auj3L._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"22","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["http://ecx.images-amazon.com/images/I/51Z632auj3L._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"56","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["http://ecx.images-amazon.com/images/I/51Z632auj3L._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"56","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["http://ecx.images-amazon.com/images/I/51Z632auj3L._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"82","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["http://ecx.images-amazon.com/images/I/51Z632auj3L._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"120","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["http://ecx.images-amazon.com/images/I/51Z632auj3L.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"375","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["http://ecx.images-amazon.com/images/I/51CaF8RpvKL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"22","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["http://ecx.images-amazon.com/images/I/51CaF8RpvKL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"56","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["http://ecx.images-amazon.com/images/I/51CaF8RpvKL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"56","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["http://ecx.images-amazon.com/images/I/51CaF8RpvKL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"82","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["http://ecx.images-amazon.com/images/I/51CaF8RpvKL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"120","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["http://ecx.images-amazon.com/images/I/51CaF8RpvKL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"375","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["http://ecx.images-amazon.com/images/I/51hqSJwF09L._SL30_.jpg"],"Height":[{"_":"22","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["http://ecx.images-amazon.com/images/I/51hqSJwF09L._SL75_.jpg"],"Height":[{"_":"56","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["http://ecx.images-amazon.com/images/I/51hqSJwF09L._SL75_.jpg"],"Height":[{"_":"56","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["http://ecx.images-amazon.com/images/I/51hqSJwF09L._SL110_.jpg"],"Height":[{"_":"82","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["http://ecx.images-amazon.com/images/I/51hqSJwF09L._SL160_.jpg"],"Height":[{"_":"120","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["http://ecx.images-amazon.com/images/I/51hqSJwF09L.jpg"],"Height":[{"_":"375","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}]},{"$":{"Category":"primary"},"SwatchImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"23","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"57","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"57","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"83","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"121","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["http://ecx.images-amazon.com/images/I/51RD5f9l6VL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"377","$":{"Units":"pixels"}}]}]}]}],"ItemAttributes":[{"Artist":["Catherine McCargar"],"Color":["Green"],"IsAdultProduct":["0"],"IsAutographed":["1"],"ItemDimensions":[{"Height":[{"_":"2400","$":{"Units":"hundredths-inches"}}],"Length":[{"_":"0","$":{"Units":"hundredths-inches"}}],"Weight":[{"_":"19","$":{"Units":"hundredths-pounds"}}],"Width":[{"_":"1800","$":{"Units":"hundredths-inches"}}]}],"MediaType":["Watercolor"],"ProductGroup":["Art"],"ProductTypeName":["FINE_ART"],"Size":["24 x 18 in."],"Title":["House with Turret"]}],"OfferSummary":[{"LowestNewPrice":[{"Amount":["127500"],"CurrencyCode":["USD"],"FormattedPrice":["$1,275.00"]}],"TotalNew":["1"],"TotalUsed":["0"],"TotalCollectible":["0"],"TotalRefurbished":["0"]}],"EditorialReviews":[{"EditorialReview":[{"Source":["Product Description"],"Content":["I found this house in the quaint town of Crockett, California. It is on a hill overlooking the Carquinez Strait. I was intrigued by the interesting design of the house, the bright summer light, and the splendid garden in full bloom. I painted a smaller piece en plein air, then later painted this one in my studio. The piece is on heavy-weight 140 lb. Arches watercolor paper. It will need a frame for display."],"IsLinkSuppressed":["0"]}]}]}]}]}}';

var obj = JSON.parse(json);
var ASIN1 = obj.ItemLookupResponse.Items[0].Item[0].ASIN[0];
document.getElementById("output").innerHTML = ASIN1;
The ASIN for the first item is: <span id="output"></span>

JSFiddle-Version: https://jsfiddle.net/0t2a3ukx/

0
Maximillian Laumeister 21 Aug. 2015 im 20:44