I've been trying to convert JSON in android. I'm getting this as a response from my socket.io-server:
[{
"_id":"5a4cee2808699d0014d99dfc",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text1",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991144450",
"__v":0
},{
"_id":"5a4cee4208699d0014d99dfd",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text2",
"author":"5a2505c50c1ff300149d799f",
"created_at":"1514991170944",
"__v":0
},{
"_id":"5a4cee8608699d0014d99dfe",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text3",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991238321",
"__v":0
},{
"_id":"5a4ceea908699d0014d99dff",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text4",
"author":"5a2505c50c1ff300149d799f",
"created_at":"1514991273472",
"__v":0
},{
"_id":"5a4ceed508699d0014d99e00",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text5",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991317353",
"__v":0
},{
"_id":"5a4ceef108699d0014d99e01",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text6;)",
"author":"5a2505c50c1ff300149d799f",
"created_at":"1514991345153",
"__v":0
},{
"_id":"5a4cef1c08699d0014d99e02",
"conversation_id":"5a4cee2808699d0014d99dfb",
"message":"text7",
"author":"5a173c64187e5c335488e906",
"created_at":"1514991388937",
"__v":0
}]
Normally one would try and get a JSONObject like this, taken from the socket.io website:
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object.. args) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
} catch (JSONException e) {
return;
}
// add the message to view
addMessage(username, message);
}
});
}
But the problem is that this is for one JSON object, so wouldn't this require a different approach since my data is an array?
I've been trying to do it the conventional way like the website said but it just returns:
java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject
Thanks in advance for trying to help me out, I'm new to this kind of stuff!
EDIT:
private Emitter.Listener onConversationJoined = new Emitter.Listener() {
@Override
public void call(final Object... args) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
String data = args[0].toString();
Log.e("TestIt", data);
JSONArray jsonarray;
try {
jsonarray = new JSONArray(args[0]);
for (int i=0; i < jsonarray.length(); i++) {
JSONObject chatmsg = jsonarray.getJSONObject(i);
String conversation_id = chatmsg.getString("conversation_id");
String message = chatmsg.getString("message");
String author = chatmsg.getString("author");
String created_at = chatmsg.getString("created_at");
addMessageUser(message);
// Whatever you want to do with these fields.
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
};
And it's saying in the LogCat:
01-03 22:04:53.758 32259-32259/com.app.app.app W/System.err: org.json.JSONException: Not a primitive array: class java.lang.String
on this line:
jsonarray = new JSONArray(args[0]);