JsonObject 和JsonArray用法进行梳理和讲解[史上复杂的JSON字符串例子来讲解]

您所在的位置:网站首页 遍历jsonarry JsonObject 和JsonArray用法进行梳理和讲解[史上复杂的JSON字符串例子来讲解]

JsonObject 和JsonArray用法进行梳理和讲解[史上复杂的JSON字符串例子来讲解]

2023-06-27 03:15| 来源: 网络整理| 查看: 265

日常生活中常见的JSON字符串处理,本篇将使用复杂的JSON字符串对JsonObject 和JsonArray用法进行梳理和讲解。

我们有一个复杂的 JSON 数据,如下所示

在看详细讲解之前,建议您先将下面的json字符串结构先在json视图解析好好看一下,将对您的理解更轻松,达到事半功倍的效果。

{ "_index": "t_spider_gat_log_20230622", "_type": "_doc", "_id": "YyUp4ogBrZfsTXIOIzYJ", "_version": 1, "_score": null, "_source": { "msg": "success", "code": 0, "responseTime": "2023-06-22T08:12:06.791Z", "channel": "", "responseJSON": "{\"code\":0,\"createTime\":\"\",\"itineraries\":[{\"prices\":[{\"bookingCode\":\"T\",\"currency\":\"NOK\",\"fareFamilyType\":\"LowFare\",\"paxType\":\"ADT\",\"price\":2110.0,\"seatCount\":9,\"tax\":482.0},{\"bookingCode\":\"T\",\"currency\":\"NOK\",\"fareFamilyType\":\"LowFare+\",\"paxType\":\"ADT\",\"price\":2560.0,\"seatCount\":9,\"tax\":482.0},{\"bookingCode\":\"A\",\"currency\":\"NOK\",\"fareFamilyType\":\"Flex\",\"paxType\":\"ADT\",\"price\":5160.0,\"seatCount\":9,\"tax\":482.0}],\"segments\":[{\"airline\":\"DY\",\"arrAirport\":\"BGO\",\"arrTerminal\":\"\",\"arrTime\":\"202310011245\",\"codeShare\":false,\"direction\":1,\"dptAirport\":\"LGW\",\"dptTerminal\":\"\",\"dptTime\":\"202310010950\",\"duration\":115,\"equipment\":\"73H\",\"flightNo\":\"DY1317\",\"go\":true,\"operatingAirline\":\"\",\"operatingFlightNo\":\"\",\"return\":false,\"stopCities\":\"\"},{\"airline\":\"DY\",\"arrAirport\":\"BGY\",\"arrTerminal\":\"\",\"arrTime\":\"202310012035\",\"codeShare\":false,\"direction\":1,\"dptAirport\":\"BGO\",\"dptTerminal\":\"\",\"dptTime\":\"202310011805\",\"duration\":150,\"equipment\":\"73H\",\"flightNo\":\"DY1848\",\"go\":true,\"operatingAirline\":\"\",\"operatingFlightNo\":\"\",\"return\":false,\"stopCities\":\"\"}]}],\"msg\":\"success\"}", "source": "SER", "reqId": "GAT_SER_f6ebd7b8771647f393fcdc4e0fd77ced", "vendorCode": "ADY", "requestJSON": "{\"application\":\"API\",\"channel\":\"\",\"condition\":{\"adtCount\":2,\"arrAirports\":[\"BGY\",\"LIN\",\"MXP\",\"PMF\"],\"arrCity\":\"MIL\",\"chdCount\":0,\"dptAirports\":[\"LCY\",\"LGW\",\"LHR\",\"LTN\",\"SEN\",\"STN\"],\"dptCity\":\"LON\",\"dptDate\":\"2023-10-01\",\"infCount\":0,\"rtnDate\":\"\",\"singleTrip\":true,\"tripType\":1},\"realTime\":false,\"region\":\"SG\",\"requestId\":\"GAT_SER_f6ebd7b8771647f393fcdc4e0fd77ced\",\"requestTime\":1687421526160,\"scriptDirectory\":\"ady\",\"source\":\"SER\",\"useProxy\":true,\"vendorCode\":\"ADY\"}", "requestTime": "2023-06-22T08:12:06.160Z", "proxy": null, "requestTimeEnd": null, "application": "API", "subOrderNumber": null, "action": "GAT", "requestTimeStart": null, "airline": null, "region": "SG", "status": "COMPLETE" }, "fields": { "requestTime": [ "2023-06-22T08:12:06.160Z" ], "responseTime": [ "2023-06-22T08:12:06.791Z" ] }, "highlight": { "responseJSON": [ "{\"code\":0,\"createTime\":\"\",\"itineraries\":[{\"prices\":[{\"bookingCode\":\"T\",\"currency\":\"NOK\",\"fareFamilyType\":\"LowFare\",\"paxType\":\"ADT\",\"@kibana-highlighted-field@price@/kibana-highlighted-field@\":2110.0,\"seatCount\":9,\"tax\":482.0},{\"bookingCode\":\"T\",\"currency\":\"NOK\",\"fareFamilyType\":\"LowFare+\",\"paxType\":\"ADT\",\"@kibana-highlighted-field@price@/kibana-highlighted-field@\":2560.0,\"seatCount\":9,\"tax\":482.0},{\"bookingCode\":\"A\",\"currency\":\"NOK\",\"fareFamilyType\":\"Flex\",\"paxType\":\"ADT\",\"@kibana-highlighted-field@price@/kibana-highlighted-field@\":5160.0,\"seatCount\":9,\"tax\":482.0}],\"segments\":[{\"airline\":\"DY\",\"arrAirport\":\"BGO\",\"arrTerminal\":\"\",\"arrTime\":\"202310011245\",\"codeShare\":false,\"direction\":1,\"dptAirport\":\"LGW\",\"dptTerminal\":\"\",\"dptTime\":\"202310010950\",\"duration\":115,\"equipment\":\"73H\",\"flightNo\":\"DY1317\",\"go\":true,\"operatingAirline\":\"\",\"operatingFlightNo\":\"\",\"return\":false,\"stopCities\":\"\"},{\"airline\":\"DY\",\"arrAirport\":\"BGY\",\"arrTerminal\":\"\",\"arrTime\":\"202310012035\",\"codeShare\":false,\"direction\":1,\"dptAirport\":\"BGO\",\"dptTerminal\":\"\",\"dptTime\":\"202310011805\",\"duration\":150,\"equipment\":\"73H\",\"flightNo\":\"DY1848\",\"go\":true,\"operatingAirline\":\"\",\"operatingFlightNo\":\"\",\"return\":false,\"stopCities\":\"\"}]}],\"msg\":\"success\"}" ] }, "sort": [ 1687421526160 ] }

为了解析这个 JSON 数据,我们首先将其转化为 JSONObject,然后在需要的地方获取嵌套的 JSONObject 或 JSONArray。

转化为 JSONObject import org.json.JSONObject; import org.json.JSONArray; // 假设 jsonString 是您提供的 JSON 字符串 // 将字符串转化为 JSONObject JSONObject jsonObj = new JSONObject(jsonString); 解析 _source

为了更好的大家理解,我这里获取最为复杂的一个字段解析 _source来讲解

// 获取 _source 的 JSONObject JSONObject source = jsonObj.getJSONObject("_source"); // 获取 _source 中的各种值 String msg = source.getString("msg"); int code = source.getInt("code"); String responseTime = source.getString("responseTime"); String channel = source.getString("channel"); 解析 responseJSON // 获取 _source 中的 responseJSON 并转化为另一个 JSONObject String responseJSONString = source.getString("responseJSON"); JSONObject responseJSON = new JSONObject(responseJSONString); 解析 itineraries // 从 responseJSON 中获取 itineraries,它是一个 JSONArray JSONArray itineraries = responseJSON.getJSONArray("itineraries"); // 遍历每一个 itinerary for (int i = 0; i // 获取每一个 price,它是一个 JSONObject JSONObject price = prices.getJSONObject(j); // 从 price 中获取各种值 String bookingCode = price.getString("bookingCode"); String currency = price.getString("currency"); String fareFamilyType = price.getString("fareFamilyType"); String paxType = price.getString("paxType"); double priceValue = price.getDouble("price"); int seatCount = price.getInt("seatCount"); double tax = price.getDouble("tax"); }

注意:此代码未处理可能的异常。在实际使用中,您可能需要包装这些调用以处理可能出现的异常,例如 JSONException

解析 segments

在每一个 itinerary 中,还有一个 segments 的 JSONArray,我们也可以解析这个数组。

// 从 itinerary 中获取 segments,它是一个 JSONArray JSONArray segments = itinerary.getJSONArray("segments"); // 遍历每一个 segment for (int k = 0; k


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3