问题描述
我只是想知道如果任何人都可以推荐一个更好的替代方法比org.json一个复杂的JSON字符串解码。作为参考,这将从Web服务器下来到Android(安培;!iOS的,但是这是其他开发人员的问题)。设备,它没有回去了。
I was just wondering if anyone could recommend a better alternative method than org.json for decoding a complex JSON string. For reference, this will be coming from a web server down to Android (& iOS, but that's the other dev's problem!) devices, it doesn't have to go back up.
的字符串是下列性质...
The string is of the following nature...
{"header":"value","count":value, ..., "messages":[
{"messagetype":1,"name":"value"},
{"messagetype":2,"name":"value","name":value},
{"messagetype":1,"name":"value"},
{"messagetype":3,"name":"value","subvalues":["value",value,value]},
...
{"messagetype":4,"name":value,"name":"value","name":value}
]}
基本上,有一些头字段,我总是可以依靠,但随后会有消息数组,变量数,内容和顺序。
Basically, there are some header fields which I can always rely on but then there will be an "array" of messages, variable in count, content and order.
我一直在研究这个了几天,并驳回GSON和其他几个人,因为这要么需要预先知道确切的结构和/或不与嵌入式类型处理好(所包含的信息)
I've been researching this for a few days now and have dismissed GSON and a few others because that either need to know the exact structure in advance and/or don't deal well with the embedded types (the contained messages).
答在指着我使用,我知道我可以使用通过字符串解析,但我想这个问题的答案的答复之一(这是超级老学校,没有人使用该库了在现实世界中的)使我怀疑我的做法。
Answer three in this question pointed me to using the org.json library and I know I can use that to parse through the string but I guess one of that answer's replies ("That's super old school and nobody uses that library anymore in the real world") has made me question my approach.
任何人都可以提出一个库/方法,它能够更好地处理这个问题?如果别人已经用另一种方法来处理这类复杂多变的结构,我真的AP preciate输入。
Can anyone suggest a library/approach which would handle this problem better? If anyone else has used an alternative approach to dealing with this type of complex and variable structure, I'd really appreciate your input.
先谢谢了。
推荐答案
我真的不约的:这是超级老学校,没有人使用该库了在现实世界中的,因为使用此库解析JSON是pretty简单。此外,能JSON变得多么复杂?我的意思是,所有关于键/值对,没有什么不能用code几行来解决,比如我将说明你几个案例,让你会得到确信是pretty简单的事:结果
假如你要从包含你在一个JSON数组需要格式化所有的信息服务器的响应,那么你可以做这样的事情来解析字符串:
I really do not agree with the opinion about org.json libray: "That's super old school and nobody uses that library anymore in the real world", since parsing json by using this library is pretty straightforward. Besides, how complex can json get?, I mean, is all about key/value pairs, nothing that can't be solved with a few lines of code, for instance I will illustrate you a few cases, so that you'll get convinced that is pretty simple to do:
Suppose you have a response from the server containing all info you need formatted in a json array, then you can do something like this to parse the String:
JsonArray arrayJson = new JsonArray(response);
但现在你要访问arrayJson孩子的:
But now you want to access arrayJson childs:
for (int i = 0; i < arrayJson.length() - 1; i++)
{
JsonObject json = arrayJson.getJSONObject(i);
}
现在,假设你有内部那些你在检索到的for循环JSON的另一个数组:
然后你会得到他们这样说:
And now assume you have another array of json's inside those you retrieved in the for loop:Then you'll get them this way:
for (int i = 0; i < arrayJson.length() - 1; i++)
{
JsonObject json = arrayJson.getJSONObject(i);
JSONArray anotherArray = json.getJSONArray("key");
}
.....,更嵌套你可以处理他们以同样的方式,所以我觉得我建立了我的观点。请记住,有时,挣扎在寻找更简单的方式做事情,可以让他们更难做。
....., more nestings you can handle them the same way, so I think I established my point. Remember that sometimes, struggling on finding easier ways to do things, can get them even harder to do.
这篇关于德code复杂的JSON字符串各种对象(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!