gpt4 book ai didi

jquery - JSON 格式问题

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

现在我正在收到 JSON url 响应,因为我想漂亮地打印此 URL

[{"chat":"ed test","displayname":"Ed Student","created_at":"2014-03-29 21:42:35"},
{"chat":"test from sue","displayname":"Sue Student","created_at":"2014-03-29 21:42:25"},{"chat":"Test from instructor","displayname":"Jane Instructor","created_at":"2014-03-29 21:42:18"}]

但我想让它看起来像这样:

[

{

"chat":"ed test",
"displayname":"Ed Student",
"created_at":"2014-03-29 21:42:35"},
{
"chat":"test from sue",
"displayname":"Sue Student",
}
]

等等。

我的代码如下:

$.ajax({

type: "POST",
url: "chatlist.php?PHPSESSID=9c2b21f6309fd0cb8c157b54eafb7381",
cache: true,
async: true,
dataType:"json",
data: JSON.stringify(messages, null, 4),
success: function(data){
$("#messages").empty();
for (var i = 0; i < data.length; i++) {
entry = data[i];
$("#messages").append("<p>"+htmlentities(entry.chat)+'<br/>&nbsp;&nbsp;'
+htmlentities(entry.displayname)+' '+htmlentities(entry.created_at)+"</p>\n");
console.log(JSON.stringify(data[i]));
}
OLD_TIMEOUT = setTimeout('messages()', 4000);
}
});

是否可以格式化我通过此 $.ajax 调用获得的响应 URL?

最佳答案

我认为您正在寻找,

JSON.stringify(value[, replacer [, space]])

喜欢,

var data = JSON.stringify(data, null, "\t"); // Using a tab character mimics standard pretty-print appearance

console.log(data);

检查

DEMO

阅读:

JSON.stringify

关于jquery - JSON 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740768/

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