jquery - 如何安排从 JSON 文件接收和发送的聊天对话数据?
问题描述
如何从 JSON 文件中获取聊天对话数据并将这些数据以适当的方式排列给特定用户。就像在任何聊天应用程序或网站中一样...我想按顺序安排发送和接收的对话...
有关更多信息,我创建了一个插图: 这就是我现在能够访问它的方式:
我使用 Jquery 访问 JSON 的 HTML 的一部分:
let chatRcd1 = obj.data['chat1'];
for(let i=0, len=chatRcd1.length; i<len; i++){
if(chatRcd1[i].from.type == "user1")
$("<p><span>" + chatRcd1[i].msg.message + "</span></p><br>").appendTo(".recieved1");
}
let chatSnt1 = obj.data['chat1'];
for(let i=0, len=chatSnt1.length; i<len; i++){
if(chatSnt1[i].from.type == "user2")
$("<p><span>" + chatSnt1[i].msg.message + "</span></p><br>").appendTo(".sent1");
let chatRcd = obj.data['chat2'];
for(let i=0, len=chatRcd.length; i<len; i++){
if(chatRcd[i].from.type == "user1")
$("<p><span>" + chatRcd[i].msg.message + "</span></p><br>").appendTo(".recieved2");
}
let chatSnt = obj.data['chat2'];
for(let i=0, len=chatSnt.length; i<len; i++){
if(chatSnt[i].from.type == "user2")
$("<p><span>" + chatSnt[i].msg.message + "</span></p><br>").appendTo(".sent2");
}
}
这是 JSON 文件数据:
{
"data":{
"chat1" : [
{
"from":{
"type":"user1"
},
"msg":{
"message": "Hello"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Hi"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "What plans for today?"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Nothing much. How about you?"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "Planning to go to a movie. Wanna come?"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Sure why not."
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "Great. see you then."
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"ya bye."
}
}
],
"chat2" : [
{
"from":{
"type":"user1"
},
"msg":{
"message": "Hi"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Hi"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "How can I help you?"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"I would like to know more about your product."
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "Sure. I will send you an email with details on our product."
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message":"Let me know if you have any doubts."
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message": "Great. Thanks!"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message":"Anytime."
}
}
]
}
}
解决方案
您可以通过逐步完成聊天并遍历单个聊天贡献,从头开始在 DOM 元素中构建 html
var chats=[];
for (var chat in dat.data){
var str='';
dat.data[chat].forEach(ch=>str+='<div class="'+ch.from.type+'">'+ch.msg.message+'</div>')
chats.push(str)
}
然后将其分配给 DOM 元素innerHTML
属性(聊天由水平线分隔<hr>
):
document.getElementById('chat').innerHTML=chats.join('<hr>');
顺便说一句:我只是喜欢jQuery,几乎可以用它做所有事情。但事实证明,这个小任务也可以用普通的“Vanilla”JavaScript 来完成!
var dat={
"data":{
"chat1" : [
{
"from":{
"type":"user1"
},
"msg":{
"message": "Hello"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Hi"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "What plans for today?"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Nothing much. How about you?"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "Planning to go to a movie. Wanna come?"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Sure why not."
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "Great. see you then."
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"ya bye."
}
}
],
"chat2" : [
{
"from":{
"type":"user1"
},
"msg":{
"message": "Hi"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"Hi"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "How can I help you?"
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message":"I would like to know more about your product."
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message": "Sure. I will send you an email with details on our product."
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message":"Let me know if you have any doubts."
}
},
{
"from":{
"type":"user2"
},
"msg":{
"message": "Great. Thanks!"
}
},
{
"from":{
"type":"user1"
},
"msg":{
"message":"Anytime."
}
}
]
}
};
var chats=[];
for (var chat in dat.data){
var str='';
dat.data[chat].forEach(ch=>str+='<div class="'+ch.from.type+'">'+ch.msg.message+'</div>')
chats.push(str)
}
document.getElementById('chat').innerHTML=chats.join('<hr>');
div {font-family: arial}
.user1 {padding:10px;background-color:#ddd;margin-right:55%; border-radius:10px}
.user2 {padding:10px;color:white;background-color:#69f;margin-left:55%; border-radius:10px}
<div id="chat"></div>
推荐阅读
- inno-setup - 仅将 Inno Setup UI 用作自解压器 - 无需安装
- r - 负回归系数,但预测值图中的正斜率
- javascript - 我无法使用函数动态设置我的 ReactJS 状态
- scikit-learn - cross_val_score 中 r2_score 和 score ='r2' 的区别
- javascript - 需要关于使用字符串使用 textwatcher 的帮助
- matlab - 在MATLAB中求解矩阵Riccati方程?
- node.js - 即使 ffmpeg 二进制文件已添加到 package.json,“找不到 FFMPEG”
- mysql - 如何选择包含选定表的新列?
- kentico - Kentico DateTime WebPart 不使用自定义时区
- webpack - Webpack删除空的html标签?