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

时间:2015-08-21 20:29:34

标签: javascript json node.js mean

解决了!谢谢大家!结果我应该直接使用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});
            });

^这会返回一个未定义的 任何帮助表示赞赏

5 个答案:

答案 0 :(得分:3)

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

由于数据已经是JavaScript对象,因此您无需为JSON.stringifyJSON.parse

而烦恼

答案 1 :(得分:2)

如果您使用原始JSON

,则为object.ItemLookupResponse.Items[0].Item[0].ASIN[0]

http://jsfiddle.net/fkLtkotm/

答案 2 :(得分:0)

如果这确实是JSON,那么大多数值实际上是数组。因此,您需要像object.ItemLookupResponse.Items[0].Item[0].ASIN[0]一样访问它们。

答案 3 :(得分:0)

您需要使用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":["$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>
&#13;
&#13;
&#13;

JSFiddle版本:https://jsfiddle.net/0t2a3ukx/

答案 4 :(得分:0)

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

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

将为您提供ASIN值