如何从 TCP/IP 服务器解析多个 JSON? [英] How to parse more then one JSON from TCP/IP server?

查看:46
本文介绍了如何从 TCP/IP 服务器解析多个 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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="}��

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

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!!

推荐答案

您可以使用 拆分 json 字符串并循环遍历数组:

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

这篇关于如何从 TCP/IP 服务器解析多个 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆