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

日常生活中常见的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 < itineraries.length(); i++) {
    // 获取每一个 itinerary,它是一个 JSONObject
    JSONObject itinerary = itineraries.getJSONObject(i);

解析 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");
}

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

解析 segments

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

// 从 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");
}

上述代码中,我们获取了 segments 数组,然后遍历了每一个 segment,并从中获取了各种值。这个过程与前面的过程类似,只不过这里涉及到的是 segments 数组,而不是 prices 数组。

总结:

这是我们如何解析复杂的 JSON 数据的过程。请注意,我们使用的是 org.json 库,这是一个非常强大且常用的处理 JSON 的 Java 库。希望这个示例对你有所帮助!

你可能感兴趣的:(json,java)