我从前端返回了JSON项目列表。当前看起来像:
[
"[{ "_id" : ObjectId("5cbcd80e0c5c9f1dfc8bf2f3"), "price" : "$1,
399.00", "name" : "AlienwareGamingPCDesktopAuroraR7-8thGenIntelCorei7-8700,
16GBDDR4Memory,
2TBHardDrive+32GBIntelOptane,
?NVIDIAGeForceGTX10808GBGDDR5X,
Windows1064bit\", "url" : "https: //www.amazon.com/gp/product/B076BHG74V/ref=s9_acsd_zwish_hd_bw_b2N0U_>c_x_w?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=merchandised-search-11&pf_rd_r=TW0CMS0QN07XT2BM838T&pf_rd_t=101&pf_rd_p=62f4ca32-9706-51c1-a1d1-9f7214564c34&pf_rd_i=565098\", "description" : "ThenewAlienwareAuroraisamid-towerdesktopwithaMicroATXmotherboardandisalsooursmallestdual-graphicscapabledesktopandhasmoretool-lessfeaturesthananypreviousAlienwaredesktopsuchas: tool-lessgraphics,
expansioncards,
hard>drives,
andmemory.", "keywords" : ["computer"] }]"
]
当使用以下命令显示结果时:console.log(results [0]),它将显示整个对象。执行console.log(results [0] .name)或console.log(results [0] .name)时,出现未定义的错误。列表中当前只有一个对象,但是对于将来的查询,将会有更多对象。关于如何访问单个元素的任何想法?
前端中的代码如下所示:
.then(response => {
this.amazonItems = response.data;
console.log(this.amazonItems[0]);
查询MongoDB:
var result = collection.Find(filter).Sort(sort).ToList().ToJson();
序列化:
List<string> returnResult = new List<string>();
returnResult.Add(_sm.SearchAmazonQuery(searchQuery, _db));
response.Content = new StringContent(JsonConvert.SerializeObject(returnResult), System.Text.Encoding.UTF8, "application/json");
response.StatusCode = HttpStatusCode.OK;
return response;
控制台输出: console
答案 0 :(得分:1)
尝试解析,我认为它处于字符串化模式。
.then(response => {
this.amazonItems = response.data;
console.log(JSON.parse(this.amazonItems[0]));
答案 1 :(得分:0)
看起来像是字符串,请尝试JSON.parse(results [0]),这也是一些怪异的语法ObjectId('asdajdjsja')
答案 2 :(得分:0)
我真的不太了解您的服务器端代码(猜测C#),但是,假设
_sm.SearchAmazonQuery(searchQuery, _db)
返回结果
var result = collection.Find(filter).Sort(sort).ToList().ToJson();
似乎已经是JSON字符串,我怀疑您只是想要
response.Content = new StringContent(_sm.SearchAmazonQuery(searchQuery, _db),
System.Text.Encoding.UTF8, "application/json");
这意味着您的response.data
将是对象数组,而不是JSON字符串数组。
.then(response => {
this.amazonItems = response.data
console.log(this.amazonItems[0])
// {_id: "5cbcd80e0c5c9f1dfc8bf2f3", price: "$1,399.00", ...
})