无法解析要发送到视图的 JSON 个值

Unable to parse JSON values to be sent to a view

已解决!谢谢大家!原来我应该直接使用 parseString 的结果。那和 JSON 对象在数组中。

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

^ 这正是我所需要的。

我在解析 JSON 对象以便将某些值发送到视图以便前端用户可以看到 JSON 中的内容时遇到了问题。我试过使用 JSON.stringify(),然后使用 JSON.parse()。这最终返回了这个:

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

原文如下JSON:http://pastebin.com/XDK9Y0gy

我需要获取的是ASIN值,可以在Items->Item->ASIN中找到。

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

^ 这个 returns 未定义 感谢任何帮助

如果这真的是JSON,大部分值实际上是数组。因此你需要像这样访问它们 object.ItemLookupResponse.Items[0].Item[0].ASIN[0].

应该是:result.ItemLookupResponse.Items[0].Item[0].ASIN[0]

因为数据已经是一个 JavaScript 对象,你不需要为 JSON.stringifyJSON.parse

操心

object.ItemLookupResponse.Items[0].Item[0].ASIN[0] 如果你使用你原来的 JSON

http://jsfiddle.net/fkLtkotm/

您将需要使用 ItemLookupResponse.Items[0].Item[0].ASIN[0];

以下是检索并显示第一个项目的 ASIN 的现场演示。

工作现场演示:

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":[",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 版本:https://jsfiddle.net/0t2a3ukx/

假设您的 json 被解析为 myJson 变量:

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

会给你ASIN值