如何从这个Json中获取消息

huangapple go评论87阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2020年4月8日 21:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61101807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定