3

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]);

MistaJoe
  • 73
  • 1
  • 7
  • what is the value of args[0] ? – diegoveloper Jan 03 '18 at 21:28
  • Your second code looks more promising, but you should test your args[0] parameter to see what it contains: `String data = args[0].toString(); Log.e("TestIt", data);` . Does the string look like you expect?? – Barns Jan 03 '18 at 21:42
  • Hello, when I log args[0].toString() it gives me the wanted string like shown in the question... It still gives me > org.json.JSONException: Not a primitive array: class java.lang.String, I have edited the question with the code I have now. – MistaJoe Jan 03 '18 at 22:28

2 Answers2

3

I fixed it by converting the "args[0].toString()" and making an JSONArray off of that, instead of just args[0].

so, instead of

jsonarray = new JSONArray(args[0]);

I had to do this:

String data = args[0].toString();
JSONArray jsonarray = new JSONArray(data);

Thanks @Barns for giving me the idea to just put in the string instead of the JSONObject.

MistaJoe
  • 73
  • 1
  • 7
0

please have a look at this stackoverflow post. you need to use JSONArray instead of JSONObject https://stackoverflow.com/a/18977220/2889043

Arun Gopinathan
  • 378
  • 4
  • 11