如何从这个Json中获取消息

huangapple 未分类评论44阅读模式
英文:

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

  1. privateChannel.listen("MeetingChatMessage", args -> {
  2. Log.i("Log", Arrays.toString(args));
  3. runOnUiThread(() -> {
  4. JSONArray jsonArray = null;
  5. try {
  6. jsonArray = new JSONArray(args);
  7. JSONObject jsonObject = jsonArray.getJSONObject(0);
  8. JSONObject jsonDATA= jsonObject.getJSONObject("data");
  9. String message = jsonDATA.getString("message");
  10. Log.i("Log", message);
  11. } catch (JSONException e) {
  12. e.printStackTrace();
  13. }
  14. });
  15. }

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

  1. [private-meeting-chat.98,
  2. {"success":true,"data":
  3. {"message":"Fuhvvhjv",
  4. "chat_message_type_id":1},
  5. "socket":null}]

here is the code i use

  1. privateChannel.listen("MeetingChatMessage", args -> {
  2. Log.i("Log", Arrays.toString(args));
  3. runOnUiThread(() -> {
  4. JSONArray jsonArray = null;
  5. try {
  6. jsonArray = new JSONArray(args);
  7. JSONObject jsonObject = jsonArray.getJSONObject(0);
  8. JSONObject jsonDATA= jsonObject.getJSONObject("data");
  9. String message = jsonDATA.getString("message");
  10. Log.i("Log", message);
  11. } catch (JSONException e) {
  12. e.printStackTrace();
  13. }
  14. });
  15. }

args comes to me, I can see it in the logs, but I can’t get information from it

答案1

得分: 0

根据您的评论,似乎 args 是一个列表/数组,其中第二个元素是来自通道的响应。因此,您需要访问该元素并将其转换为 JSON。

  1. JSONObject jsonObject = new JSONObject(args[1]);
  2. JSONObject jsonDATA= jsonObject.getJSONObject("data");
  3. 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.

  1. JSONObject jsonObject = new JSONObject(args[1]);
  2. JSONObject jsonDATA= jsonObject.getJSONObject("data");
  3. 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://java.coder-hub.com/61101807.html
匿名

发表评论

匿名网友

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

确定