0

Source:

try {
  JSONArray jsonArray = new JSONArray(intent.getStringExtra("chatData"));

  for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject item = jsonArray.getJSONObject(i);
    messagesArr = new JSONArray(item.getString("message"));
    messagesObj = new JSONObject();
    messagesObj.put("messages", messagesArr);
    populateMessages(messagesObj);
  }
} catch (Exception e) {

}

I've also attempted changing the following line with no success:

messagesArr = new JSONArray(item.getJSONObject("message"));

Any suggestions are appreciated.

Values:

item =  {"message":"User has joined.","type":"agent","created":"2016-11-21 20:55:22","name":"Username"}

LogCat:

1-21 21:15:40.775 23532-23532/com.example.examplemobile W/System.err: org.json.JSONException: Value Johnny of type java.lang.String cannot be converted to JSONArray
11-21 21:15:40.775 23532-23532/com.example.examplemobile W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
11-21 21:15:40.785 23532-23532/com.example.examplemobile W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
11-21 21:15:40.785 23532-23532/com.example.examplemobile W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
  • 1
    Your JSON has no array, it looks like JSONObject – Raghav Nov 22 '16 at 02:03
  • Note - when changing to messagesArr = new JSONObject(item.getString("message")); I get the following error: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject – Droidthusiast Nov 22 '16 at 02:25
  • What does logging intent.getStringExtra("chatData") print out? Are you sure that your Intent.putExtra() is putting in valid JSON? – Glen Pierce Nov 22 '16 at 02:30
  • +1 @iNan. @droidthusiast: Your logcat says `Value Johnny of type java.lang.String cannot be converted to JSONArray`, it means that either you get the wrong json data or you read the json data string as array . – ישו אוהב אותך Nov 22 '16 at 02:33
  • TAG: [{"type":"agent","message":"User has joined the chat.","created":"2016-11-21 21:40:31","name":"User"},{"type":"agent","message":"example message,"created":"2016-11-21 21:40:36","name":"User"},{"type":"agent","message":"User has left the chat.","created":null,"name":"User"}] (apologies - I was swapping out the actual users name with something generic after the fact) – Droidthusiast Nov 22 '16 at 02:42
  • I'm trying to read it as JSON Object instead of an array but I'm still getting errors (I apologize if I'm overlooking something obvious - it's getting late) http://pastebin.com/B0pQ34F2 – Droidthusiast Nov 22 '16 at 02:55

3 Answers3

1

You're trying to parse a String as a JSONArray, that's why it's throwing the exception. In order to parse it as a JSONArray, you'd have to have data like this: { "message":["String1", "String2", "String3"] }

Change your messagesArr variable to a String, and then access the "message" string by calling messagesArr = item.getString("message");

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
1

From your json string that you provided on your comment, it is not a valid json string

[{"type":"agent","message":"User has joined the chat.","created":"2016-11-21 21:40:31","name":"User"},{"type":"agent","message":"example message,"created":"2016-11-21 21:40:36","name":"User"},{"type":"agent","message":"User has left the chat.","created":null,"name":"User"}]

It needs a double quotation mark after example message

Try to change the json string to

[
  {
    "type": "agent",
    "message": "User has joined the chat.",
    "created": "2016-11-21 21:40:31",
    "name": "User"
  },
  {
    "type": "agent",
    "message": "example message",
    "created": "2016-11-2121: 40: 36",
    "name": "User"
  },
  {
    "type": "agent",
    "message": "Userhasleftthechat.",
    "created": null,
    "name": "User"
  }
]

and let me know the result

HendraWD
  • 2,984
  • 2
  • 31
  • 45
0

I think you need to convert your String JSON intent.getStringExtra("chatData")to JSON Array.

Maybe this link can help Convert String To JSON Array

Community
  • 1
  • 1
Cesario
  • 75
  • 1
  • 9