为了账号安全,请及时绑定邮箱和手机立即绑定

使用 jsonpath 解析数组索引

使用 jsonpath 解析数组索引

慕斯709654 2023-03-31 17:06:40
我有一个 JSON 文件,其中包含许多对象和数组以及数组中的对象。我在 json 路径中可访问的路由对象中有源和目的地之间的路由列表 - data.offer.travels[1].routes。有 15 个这样的路线对象。在每个路线对象中都有一个名为 legs 的数组,其长度可以是 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有人可以建议我哪里出错了吗?
查看完整描述

1 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

您需要使用 带有更多示例的jsonPath.param("i",i).get ... 


查看完整回答
反对 回复 2023-03-31
  • 1 回答
  • 0 关注
  • 270 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信