Common JSON string processing in daily life, this article will use complex JSON strings to sort out and explain the usage of JsonObject and JsonArray.
We have a complex JSON data as below
Before reading the detailed explanation, it is recommended that you parse the following json string structure in the json view to take a good look, which will make your understanding easier and achieve twice the result with half the effort.
{
"_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
]
}
To parse this JSON data, we first convert it to JSONObject, and then get nested JSONObject or JSONArray where needed.
Convert to JSONObject
import org.json.JSONObject;
import org.json.JSONArray;
// 假设 jsonString 是您提供的 JSON 字符串
// 将字符串转化为 JSONObject
JSONObject jsonObj = new JSONObject(jsonString);
parsing_source
In order to better understand everyone, I will get the most complicated field analysis _source to explain
// 获取 _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");
parse responseJSON
// 获取 _source 中的 responseJSON 并转化为另一个 JSONObject
String responseJSONString = source.getString("responseJSON");
JSONObject responseJSON = new JSONObject(responseJSONString);
analysis itineraries
// 从 responseJSON 中获取 itineraries,它是一个 JSONArray
JSONArray itineraries = responseJSON.getJSONArray("itineraries");
// 遍历每一个 itinerary
for (int i = 0; i < itineraries.length(); i++) {
// 获取每一个 itinerary,它是一个 JSONObject
JSONObject itinerary = itineraries.getJSONObject(i);
parsing prices
// 从 itinerary 中获取 prices,它是一个 JSONArray
JSONArray prices = itinerary.getJSONArray("prices");
// 遍历每一个 price
for (int j = 0; j < prices.length(); j++) {
// 获取每一个 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");
}
NOTE: This code does not handle possible exceptions. In practice, you may need to wrap these calls to handle possible exceptions such as JSONException
parsing segments
In each itinerary, there is also a JSONArray of segments, and we can also parse this array.
// 从 itinerary 中获取 segments,它是一个 JSONArray
JSONArray segments = itinerary.getJSONArray("segments");
// 遍历每一个 segment
for (int k = 0; k < segments.length(); k++) {
// 获取每一个 segment,它是一个 JSONObject
JSONObject segment = segments.getJSONObject(k);
// 从 segment 中获取各种值
String airline = segment.getString("airline");
String arrAirport = segment.getString("arrAirport");
String arrTerminal = segment.getString("arrTerminal");
String arrTime = segment.getString("arrTime");
boolean codeShare = segment.getBoolean("codeShare");
int direction = segment.getInt("direction");
String dptAirport = segment.getString("dptAirport");
String dptTerminal = segment.getString("dptTerminal");
String dptTime = segment.getString("dptTime");
int duration = segment.getInt("duration");
String equipment = segment.getString("equipment");
String flightNo = segment.getString("flightNo");
boolean go = segment.getBoolean("go");
String operatingAirline = segment.getString("operatingAirline");
String operatingFlightNo = segment.getString("operatingFlightNo");
boolean returnFlag = segment.getBoolean("return");
String stopCities = segment.getString("stopCities");
}
In the above code, we obtained the segments array, then traversed each segment, and obtained various values from it. This process is similar to the previous process, except that the segments array is involved instead of the prices array.
Summarize:
This is how we parse complex JSON data. Note that we are using the org.json library, which is a very powerful and commonly used Java library for manipulating JSON. Hope this example helps you!