英文:
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论