gpt4 book ai didi

javascript - 访问 JSON 对象数组的 JSON 元素

转载 作者:行者123 更新时间:2023-12-02 23:40:24 25 4
gpt4 key购买 nike

我正在从前端返回一个 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

最佳答案

尝试解析,我认为它处于字符串化模式。

.then(response => {
this.amazonItems = response.data;
console.log(JSON.parse(this.amazonItems[0]));

关于javascript - 访问 JSON 对象数组的 JSON 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com