使用JSONPath遍历大型JSON数组 [英] Iterate over a large JSON Array with JSONPath

查看:2083
本文介绍了使用JSONPath遍历大型JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Java应用程序,该应用程序需要遍历大型JSON数组(包含约2万个项目),并且在每个数组中解析一个子数组.每个项目看起来像这样:

I have a simple Java app that needs to traverse a large JSON array (with around 20K items), and within each array, I parse a sub-array. Each item looks like this:

{"index":0,"f1":[2,16,16,16,16,16,32,16],"f2":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"startTime":0.0}

我正在使用JSONPath遍历每个项目.我要做的是首先读取长度,然后简单地遍历整个数组.但这非常慢(例如每秒1个项目).

I am using JSONPath to iterate over each item. What I do is first I read the length, and simply iterate over the whole array. But it is very slow (like, 1 item per second).

int length = JsonPath.read(response, "$.result.length()");
for (int i = 0; i < length; i++) {
    double start_time = JsonPath.read(response, "$.result["+i+"].startTime");
    ArrayList<Integer> f1= JsonPath.read(response, "$.result["+i+"].f1");
    //...other things
}

有没有一种优化方法?

推荐答案

知道了.多亏了Erwin,我可以将整个JSON一次解析成HASHMap,就像这样:

Got it. Thanks to Erwin, I can parse the whole JSON at once into a HASHMap simply like this:

ArrayList<HashMap> json= JsonPath.read(response, "$.result");

然后我们可以简单地调用get(i)来访问循环中的特定项目:

And then we can simply call get(i) to access a specific item in the loop:

double start_time = (double) json.get(i).get("startTime");

这篇关于使用JSONPath遍历大型JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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