gpt4 book ai didi

javascript - 解析 JSON 数据的优化方式

转载 作者:行者123 更新时间:2023-11-28 21:03:21 24 4
gpt4 key购买 nike

我有一个 JSON 数据

[
{
"Name": "Tom",
"Email": "tom@gmail.com",
"Notes": "Yea, it's good",
"Ratings": "5",
"Messages": [
"Tom - I am good",
"System - How're you doing?",
"Tom - True, that!",
"System - Oh Yes! ;) "
]
},
{
"Name": "Sunny",
"Email": "sunny@gmail.com",
"Notes": "Yea, it's good",
"Ratings": "2",
"Messages": [
"Sunny-I am good",
"System - How're you doing?",
"Sunny - True, that!",
"System - Oh Yes! ;) "
]
}
]

我正在使用 jQuery 解析它并像这样迭代它;

            var jsonData = $("#jsonData").val();
var plainData = $.parseJSON(jsonData);

for (var j = 0; j < plainData.length; j++) {
var Name = plainData[j].Name;
var Email = plainData[j].Email;
var Notes = plainData[j].Notes;
var Ratings = plainData[j].Ratings;

var Messages = plainData[j].Messages;
var _messages = " ";
for (var i = 0; i < Messages.length; i++)
_messages += Messages[i] + "\n";

}

我不太确定在 for 循环中迭代它是否是一个好主意。谁能建议我一种更好/优化的方法来迭代解析的 json 值?

最佳答案

您刚才的做法是最有效的方法,除了:

var Messages = plainData[j].Messages;
var _messages = " ";
for (var i = 0; i < Messages.length; i++)
_messages += Messages[i] + "\n";

也可以写得更好:

_messages = plainData[j].Messages.join('\n') + '\n';

关于javascript - 解析 JSON 数据的优化方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480853/

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