[英]Parsing with array indices with jsonpath
我有一个JSON文件,其中包含许多对象和数组以及数组中的对象。
我在json路径中可访问的routes对象中有一个源与目的地之间的路由列表-data.offer.travels [1] .routes。 据说有15条这样的路线对象。
在每个路由对象中,都有一个称为支路的数组,其长度可以为1、2或3。我编写了带有for循环的方法,以在路由对象内循环并返回只有一条支路的第一个路由索引。 但它给出了一个错误。
为此编写了一个for循环。 这是代码
public JSONObject PrepareProvBookingRequestBody() throws IOException, ParseException {
File jsonExample = new File(System.getProperty("user.dir"),"\\JSONOutputFiles\\ThreeAdults_TwoCh_StdRet_PUB\\ThreeAdults_TwoCh_StdRet_PUB_JS.json");
JsonPath jsonPath = new JsonPath(jsonExample);
int routeindexOutbound=0;
int routeindexInbound=0;
List<Object> routesOutbound = jsonPath.getList("data.offer.travels[1].routes");
int NoOfOutboundRoutes= routesOutbound.size();
for (int i=0; i<NoOfOutboundRoutes; i++)
{
JsonArray legs = jsonPath.get("data.offer.travels[1].routes[i].legs");
int NoOfLegs = legs.size();
if (NoOfLegs==1) {
routeindexOutbound=i;
break;
}
}
String DepartureDate = jsonPath.getString("data.offer.travels[1].routes[routeindexOutbound].legs[0].service_schedule_date");
这是我得到的错误
java.lang.IllegalArgumentException: The parameter "i" was used but not
defined. Define parameters using the JsonPath.params(...) function
有人可以对我在哪里出问题提出建议吗?
您需要使用jsonPath.param("i",i).get ...
定义参数jsonPath.param("i",i).get ...
此文档包含更多示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.