简体   繁体   中英

Parsing with array indices with jsonpath

I have a JSON file with many objects and arrays and objects within the arrays.

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. There are say 15 such routes objects.

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. But it gives an error.

Written a for loop to do this. 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 ...文档包含更多示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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