[英]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.