从CloudMQTT解析字符串时出现JSON异常

时间:2017-12-24 12:58:55

标签: android json

我正在尝试解析从MQTT代理接收到的JSON,并将其显示到回收器视图中,但在尝试创建JSON对象时我不断收到异常。为什么会出现此问题,我需要做些什么来解决它?

 public void messageArrived(String topic, MqttMessage mqttMessage)  {
            progressDialog.dismiss();

            Log.w("Debug", mqttMessage.toString());
            json_string = mqttMessage.toString();

            try {
                jsonObject = new JSONObject(json_string); //Problem Here?
                jsonArray = jsonObject.getJSONArray("message");

                for (int i =0; i<jsonArray.length(); i++) {
                    JSONObject o = jsonArray.getJSONObject(i);
                    List_Item item = new List_Item(
                            o.getString("subject"),
                            o.getString("body")
                    );
                    listItems.add(item);
                }
                adapter = new MyAdapter(listItems, getApplicationContext());
                recyclerView.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

这是我发送给应用程序的JSON的副本:

    {“message”:[{“subject”:”Test 1”, “body”:”This is a Test.”},{“subject”:”Test 2”, “body”:”This is a Test.”},{“subject”:”Test 3”, “body”:”This is a Test.”},{“subject”:”Test 4”, “body”:”This is a Test.”}]}

1 个答案:

答案 0 :(得分:0)

问题只不过是引用。给定示例包含,而不是"