Search code examples
androidsocketstcp

How to parse more then one JSON from TCP/IP server?


How to parse more then one JSON which each ending with null character(through socket TCP/IP).

{"ObjectID":"UHJvY1dpcmVsZXNzTXNn","DeviceCode":"RUNEOjI=","ActiveInputNames":"Q2hlY2sgaW4gRmFpbA==","DeviceInputNo":"999999","Activation":false,"Reset":true,"LocationID":"","LocationGroupText":"","ProtocolText":"","CallBackNo":"OTE5MTgyNTcyMjQ5"}��{"ObjectID":"VFBpbmdPYmplY3Q="}��

As you can see the above response which has 2 JSON's each ending with null character...I can easily parse the single JSON but unable to parse more then one JSON..

It would be great if any one suggest any solutions!!


Solution

  • You can split the json string using the �� and loop through the array:

    String s = "{\"ObjectID\":\"UHJvY1dpcmVsZXNzTXNn\",\"DeviceCode\":\"RUNEOjI=\",\"ActiveInputNames\":\"Q2hlY2sgaW4gRmFpbA==\",\"DeviceInputNo\":\"999999\",\"Activation\":false,\"Reset\":true,\"LocationID\":\"\",\"LocationGroupText\":\"\",\"ProtocolText\":\"\",\"CallBackNo\":\"OTE5MTgyNTcyMjQ5\"}��{\"ObjectID\":\"VFBpbmdPYmplY3Q=\"}��";
                String[] array = s.split("��");
                for (String string: array){
                    try {
                        JSONObject json = new JSONObject(string);
                        //do what ever you want with this
                    } catch (JSONException e) {
                        Log.e("Error",Log.getStackTraceString(e));
                    }
                }