用Jackson解析JSON [英] Parsing JSON with Jackson
问题描述
我使用 Jackson 从json inputStream
中解析JSON,如下所示:
[
[36,
100,
3n + 1问题,
56717,
0 ,
1000000000,
0,
6316,
0,
0,
88834,
0,
45930,
0,
46527,
5209,
200860,
3597,
149256,
3000,
1
],
[
........
],
[
........
],
..... //和几乎5000个数组,如
]
是原始供稿链接: http://uhunt.felix-halim.net/api/p
我想解析它并只保留每个数组的前4个元素d跳过其他18个元素。
36
100
3n + 1问题
56717
code>
目前为止我已经尝试过的代码结构:
while(jsonParser.nextToken()!= JsonToken.END_ARRAY){
jsonParser.nextToken(); //'['
while(jsonParser.nextToken()!= JsonToken.END_ARRAY){
//我在这里尝试了很多方法,但找不到合适的
}
$ b $ p
$ b由于这个提要非常大,我需要使用更少的开销和内存。
还有三种模式可以处理JSON:流式API ,数据绑定和树模型。哪一个适合我的目的?
我怎样才能有效地解析这个json与杰克逊?如何跳过这18个元素并跳到下一个数组以获得更好的性能?
(解决方案)
Jackson
和GSon
两者几乎都以相同机制工作(增量模式,内容是逐步读写的),因为它有一个函数skipValue()
,所以我切换到GSON
与名字)。虽然Jackson的nextToken()
会像skipValue()
,GSON
对我来说似乎更加灵活。感谢@Kowser兄弟的推荐,之前我了解了GSON,但不知何故忽略了它。这是我的工作代码:reader.beginArray();
while(reader.hasNext()){
reader.beginArray();
int a = reader.nextInt();
int b = reader.nextInt();
String c = reader.nextString();
int d = reader.nextInt();
System.out.println(a ++ b ++ c ++ d);
while(reader.hasNext())
reader.skipValue();
reader.endArray();
}
reader.endArray();
reader.close();
解决方案
Jackson
按照本教程。
明智地使用jasonParser.nextToken()应该可以帮到你。
while(jasonParser.nextToken()!= JsonToken.END_ARRAY){//可能是JsonToken.START_ARRAY?
伪代码是
- 查找下一个数组
- 读取值
这是为
gson
。
查看本教程一>。考虑以下第二个示例。
明智地使用
reader.begin *
reader .end *
和reader.skipValue
应该为您完成这项工作。
I am using Jackson to parse JSON from a json
inputStream
which looks like following:[ [ 36, 100, "The 3n + 1 problem", 56717, 0, 1000000000, 0, 6316, 0, 0, 88834, 0, 45930, 0, 46527, 5209, 200860, 3597, 149256, 3000, 1 ], [ ........ ], [ ........ ], .....// and almost 5000 arrays like above ]
This is the original feed link: http://uhunt.felix-halim.net/api/p
I want to parse it and keep only the first 4 elements of every array and skip other 18 elements.
36 100 The 3n + 1 problem 56717
Code structure I have tried so far:
while (jsonParser.nextToken() != JsonToken.END_ARRAY) { jsonParser.nextToken(); // '[' while (jsonParser.nextToken() != JsonToken.END_ARRAY) { // I tried many approaches here but not found appropriate one } }
As this feed is pretty big, I need to do this efficiently with less overhead and memory. Also there are three models to procress JSON: Streaming API, Data Binding and Tree Model. Which one is appropriate for my purpose?
How can I parse this json efficiently with Jackson? How can I skip those 18 elements and jump to next array for better performance?
Edit: (Solution)
Jackson
andGSon
both works in almost in the same mechanism (incremental mode, since content is read and written incrementally), I am switching toGSON
as it has a functionskipValue()
(pretty appropriate with name). Although Jackson'snextToken()
will work likeskipValue()
,GSON
seems more flexible to me. Thanks @Kowser bro for his recommendation, I came to know about GSON before but somehow ignored it. This is my working code:reader.beginArray(); while (reader.hasNext()) { reader.beginArray(); int a = reader.nextInt(); int b = reader.nextInt(); String c = reader.nextString(); int d = reader.nextInt(); System.out.println(a + " " + b + " " + c + " " + d); while (reader.hasNext()) reader.skipValue(); reader.endArray(); } reader.endArray(); reader.close();
解决方案This is for
Jackson
Follow this tutorial.
Judicious use of jasonParser.nextToken() should help you.
while (jasonParser.nextToken() != JsonToken.END_ARRAY) { // might be JsonToken.START_ARRAY?
The pseudo-code is
- find next array
- read values
- skip other values
- skip next end token
This is for
gson
. Take a look at this tutorial. Consider following second example from the tutorial.Judicious use of
reader.begin*
reader.end*
andreader.skipValue
should do the job for you.And here is the documentation for JsonReader
这篇关于用Jackson解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!