JSON数据提取和写入DIV|聊天和通话APP| ZegoCloud

vc9ivgsu  于 2022-12-15  发布在  Go
关注(0)|答案(1)|浏览(137)

newbie here, I need help with a Chat integration:
Here is a Function which I use to send message:

ZegoExpressManager.shared.sendBroadcastMessage(
          config.roomID,
          "hello world!"
        );

With this one I receive it to my ConsoleLog:

ZegoExpressManager.shared.onIMRecvBroadcastMessage(
        async (roomID, messageInfo) => {
        console.log("onIMRecvBroadcastMessage", roomID, messageInfo);}

ConsoleOutput:

{"time":"2022/07/09 12:34:40.611","level":1,"action":"zm.lrh.hsp","content":"push {\"header\":{\"sub_cmd\":\"/lr/push/im_chat\",\"msg_id\":\"1381362731MBy20xcWHa212374460\",\"send_time\":\"1657362880492\",\"room_id\":\"MBy20xcWHa\",\"sender_user_nid\":\"12443410347985956212\"},\"body\":{\"msg_data\":[{\"id_name\":\"935384446\",\"nick_name\":\"web-935384446\",\"msg_id\":\"31\",\"msg_category\":1,\"msg_type\":1,\"msg_priority\":1,\"msg_content\":\"\\hello world!\",\"send_time\":\"1657362880490\"}],\"room_id\":\"MBy20xcWHa\",\"server_msg_id\":\"31\",\"ret_msg_id\":\"31\"}}","appid":1381362731,"roomid":"MBy20xcWHa","userid":"719676606","userName":"","sessionid":""}

Now i need to extract "msg_content" and write it into a div
Thank You !

  • Here is Console Output as Text*

{"time":"2022/07/09 12:34:40.611","level":1,"action":"zm.lrh.hsp","content":"push {"header":{"sub_cmd":"/lr/push/im_chat","msg_id":"1381362731MBy20xcWHa212374460","send_time":"1657362880492","room_id":"MBy20xcWHa","sender_user_nid":"12443410347985956212"},"body":{"msg_data":[{"id_name":"935384446","nick_name":"web-935384446","msg_id":"31","msg_category":1,"msg_type":1,"msg_priority":1,**"msg_content":"\hello+world"**,"send_time":"1657362880490"}],"room_id":"MBy20xcWHa","server_msg_id":"31","ret_msg_id":"31"}}","appid":1381362731,"roomid":"MBy20xcWHa","userid":"719676606","userName":"","sessionid":""}

THIS is What im Trying
JSON.parse()

const message = JSON.parse(messageInfo);
      document.write(message.msg_content);

Output

[{"fromUser":{"userID":"72219676606","userName":"web-72219676606"},"message":"\nhallooo","sendTime":1657368483227,"messageID":55}]

@uminder 's Solution ends with following stacktrace

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'substring') at call.php?userID=9985SAS720&roomID=MZ9gK86EYN:266:40 at ZegoExpressManager.js:2:1299729 at Array.forEach () at e.actionListener (ZegoExpressManager.js:2:1299702) at e.handlePushRoomMsg (ZegoExpressManager.js:2:824050) at e.h [as handlePushRoomMsg] (ZegoExpressManager.js:2:814651) at e.handleSwitchPush (ZegoExpressManager.js:2:1334477) at e.handlePush (ZegoExpressManager.js:2:1333077) at ZegoExpressManager.js:2:1330411 at ZegoExpressManager.js:2:1139037 (anonymous) @ call.php?userID=9985SAS720&roomID=MZ9gK86EYN:266 (anonymous) @ ZegoExpressManager.js:2 e.actionListener @ ZegoExpressManager.js:2 e.handlePushRoomMsg @ ZegoExpressManager.js:2 h @ ZegoExpressManager.js:2 e.handleSwitchPush @ ZegoExpressManager.js:2 e.handlePush @ ZegoExpressManager.js:2 (anonymous) @ ZegoExpressManager.js:2 (anonymous) @ ZegoExpressManager.js:2 setTimeout (async) (anonymous) @ ZegoExpressManager.js:2 e.emit @ ZegoExpressManager.js:2 e.onPushEvent @ ZegoExpressManager.js:2 e.onPushEvent @ ZegoExpressManager.js:2 onPushEvent @ ZegoExpressManager.js:2 socketService.onMessage @ ZegoExpressManager.js:2 socket.socket.onmessage @ ZegoExpressManager.js:2

iibxawm4

iibxawm41#

首先必须提取、规范化和parse相关的嵌套JSON

const content = messageInfo.content;
const contentJSON = JSON.parse(content.substring(content.indexOf('{')).replace(/\\/g, ''));

然后,您可以访问msg_content。由于msg_dataarray,因此您可能会找到其中的几个。

const msgContent = contentJSON.body.msg_data.map(v => v.msg_content).join('\n');

最后,将结果赋给divinnerText

document.querySelector('#msgContent').innerText = msgContent;

请看下面的代码,看看它是如何工作的.
一个三个三个一个

相关问题