简体   繁体   English

使用jsonpath解析数组索引

[英]Parsing with array indices with jsonpath

I have a JSON file with many objects and arrays and objects within the arrays. 我有一个JSON文件,其中包含许多对象和数组以及数组中的对象。

I have a list of routes between a source and destination in the routes object accessible in the json path - data.offer.travels[1].routes. 我在json路径中可访问的routes对象中有一个源与目的地之间的路由列表-data.offer.travels [1] .routes。 There are say 15 such routes objects. 据说有15条这样的路线对象。

Within each route object there is an array called legs whose length can be either 1, 2 or 3. I have written the method with a for loop to loop within routes object and return the first route index that had only 1 leg. 在每个路由对象中,都有一个称为支路的数组,其长度可以为1、2或3。我编写了带有for循环的方法,以在路由对象内循环并返回只有一条支路的第一个路由索引。 But it gives an error. 但它给出了一个错误。

Written a for loop to do this. 为此编写了一个for循环。 Here is the code 这是代码

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");

This is the error I get 这是我得到的错误

java.lang.IllegalArgumentException: The parameter "i" was used but not 
defined. Define parameters using the JsonPath.params(...) function

Can someone kindly advice on where am I going wrong here? 有人可以对我在哪里出问题提出建议吗?

您需要使用jsonPath.param("i",i).get ...定义参数jsonPath.param("i",i).get ...文档包含更多示例

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

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