简体   繁体   English

Android:如何以不同的方式从json对象获取jsonarray

[英]Android : how to get jsonarray from json object in different ways

I know how to get jsonarray from jsonobject . 我知道如何让jsonarrayjsonobject I am doing like below code to get jsonarray. 我正在做下面的代码来获取jsonarray。

JSONObject recvJson = new JSONObject(holder.toString());   
String numberByte= (String) recvJson.get("data");
String ts = (String) recvJson.get("time");
JSONObject temp2 = new JSONObject("{ \"data\" : " + numberByte+ "}");
JSONArray recvJarray = temp2.getJSONArray("data"); 

for (int i = 0; i < recvJarray.length(); i++) 
{
byteArray[i] = (byte) recvJarray.getInt(i); 
}

But don't want to use for-loop , without using for-loop or any other loop want to retrieve jsonarray data values . 但是不想使用for-loop ,不使用for循环任何其他循环想要检索jsonarray数据值

How should I do ?? 我应该怎么做 ?? I have done google & saw many forums but dint succeed to retrieve data without using for-loop. 我已经完成谷歌和看到很多论坛,但dint成功检索数据而不使用for-loop。

for example : I ll be getting 80 - 100 packets of 1024bytes per second from server, I want to retrieve this data & store it into bytearray . 例如: I ll be getting 80 - 100 packets of 1024bytes per second from server, I want to retrieve this data & store it into bytearray By usingf for-loop its taking around 300ms- 400ms and I am loosing many packets between that. 通过使用f for-loop,它需要大约300ms到400ms,我在它之间丢失了很多数据包。 So I want to use different approach. 所以我想用不同的方法。 If any Idea or solution for cracking this. 如果有任何想法或解决方案来破解这个。

Help will be appreciated !! 帮助将不胜感激!

try usig gson 试试usig gson

it uses java reflection to convert objects to json and json to object with simple methods 它使用java反射将对象转换为json,使用简单的方法将json转换为对象

toJson(),fromJson() (and it works for object arrays and lists too) toJson(),fromJson()(它也适用于对象数组和列表)

but you have to write the proper classes for the jsons (with all fields) 但是你必须为jsons编写适当的类(包含所有字段)

read the documentation. 阅读文档。

You can use GSON to parse the json objects. 您可以使用GSON来解析json对象。 It is much faster and easier to decode json data. 解码json数据要快得多,也更容易。 Check these links. 检查这些链接。 Gson1 , Gson2 Gson1Gson2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM