javascript - 无法解析要发送到 View 的 JSON 值

标签 javascript json node.js mean

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

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

^ 这正是我所需要的。

我在解析 JSON 对象以便将某些值发送到 View 以便前端用户可以看到 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});
            });

^ 这将返回一个未定义的 感谢任何帮助

最佳答案

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

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

关于javascript - 无法解析要发送到 View 的 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32148796/

相关文章:

javascript - 如何根据node.js中的当前时间戳生成带有年份的动态季度数字?

json - 如何为包含 Properties 对象的对象定义 JSON 模式?

在 C 中将 Protocol Buffer 与 JSON 相互转换,而不生成 C 代码

javascript - 未捕获的类型错误 : Cannot set property 'innerHTML' of null in javascript

javascript - 在 React Native 应用程序中监听任意位置的点击

javascript - Azure AD V2/msal.js 限制个人账户?

android - Android ListView 中的 JSONArray

javascript - 加载 View 后express.js传递变量

javascript - node js - 从回调中获取结果

node.js - 如何使用 Node js 创建条件调试 Visual Studio Code