如何从TCP / IP服务器解析多于一个的JSON?

时间:2019-07-01 10:24:12

标签: android sockets tcp

如何解析多于一个以空字符结尾的JSON(通过套接字TCP / IP)。

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

如您所见,上面的响应包含2个 JSON ,每个结尾都为空字符...我可以轻松地解析单个 JSON ,但无法解析多个 JSON ..

如果有人提出任何解决方案,那就太好了!

1 个答案:

答案 0 :(得分:0)

您可以使用��分割json字符串并遍历数组:

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