英文:
how to get a message from this Json
问题
I listen to the socket and it comes Json, but I can't parse it to get a message
privateChannel.listen("MeetingChatMessage", args -> {
    Log.i("Log", Arrays.toString(args));
    runOnUiThread(() -> {
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(args);
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject jsonDATA= jsonObject.getJSONObject("data");
            String message = jsonDATA.getString("message");
            Log.i("Log", message);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    });
}
args comes to me, I can see it in the logs, but I can't get information from it
英文:
I listen to the socket and it comes Json, but I can’t parse it to get a message
[private-meeting-chat.98, 
{"success":true,"data":
{"message":"Fuhvvhjv",
"chat_message_type_id":1},
"socket":null}]
here is the code i use
privateChannel.listen("MeetingChatMessage", args -> {
            Log.i("Log", Arrays.toString(args));
            runOnUiThread(() -> {
                JSONArray jsonArray = null;
                try {
                    jsonArray = new JSONArray(args);
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    JSONObject jsonDATA= jsonObject.getJSONObject("data");
                    String message = jsonDATA.getString("message");
                    Log.i("Log", message);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            });
        }
args comes to me, I can see it in the logs, but I can’t get information from it
答案1
得分: 0
根据您的评论,似乎 args 是一个列表/数组,其中第二个元素是来自通道的响应。因此,您需要访问该元素并将其转换为 JSON。
JSONObject jsonObject = new JSONObject(args[1]);
JSONObject jsonDATA= jsonObject.getJSONObject("data");
String message = jsonDATA.getString("message");
另外,由于它没有正常工作,您是否检查了错误堆栈跟踪是什么?
英文:
From your comments, it seems that args is a list/array where the 2nd element is the response from the channel. So you need to access that element and convert to json.
JSONObject jsonObject = new JSONObject(args[1]);
JSONObject jsonDATA= jsonObject.getJSONObject("data");
String message = jsonDATA.getString("message");
Also, since it's not working, did you check what the error stacktrace is?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论