简体   繁体   English

从 Java - JSON 解析中的 json 获取键值

[英]Get the key value from a json in Java - JSON Parsing

I have a json as below.我有一个 json 如下。 I want to get mobile_number from this jsonObject.我想从这个 jsonObject 中获取 mobile_number。

json:- json:-

{"id": "ABCD", "report": { "data": { "phone": { "mobile_number": 9876543210, "active": "Y", "content": null } } } }

I am doing it like this and it works fine but can someone help me with any other approach for it without getting every key.我正在这样做,并且效果很好,但是有人可以在不获取所有密钥的情况下帮助我使用任何其他方法。

JSONObject jsonObject = new JSONObject(json);
JSONObject report = getJSONObjectFromJson(jsonObject, "report");
JSONObject data = getJSONObjectFromJson(jsonObject, "data");
JSONObject phone = getJSONObjectFromJson(data, "phone");
long mobileNumber = getLongFromJson(phone, "mobile_number");

private Long getLongFromJson(JSONObject object, String key){
    return (object !=null && object.has(key)) ? object.getLong(key) : null;
}

private JSONObject getJSONObjectFromJson(JSONObject object, String key){
    return (object !=null && object.has(key)) ? object.getJSONObject(key) : null;
}

I've just dealing with the similar issue and decided to use JsonPath like this:我刚刚处理了类似的问题,并决定像这样使用 JsonPath:

final DocumentContext jsonContext = JsonPath.parse(jsonString);
final Object read = jsonContext.read("$['report']['data']['phone']['mobile_number']");

You can use Jackson ObjectMapper.您可以使用 Jackson ObjectMapper。

        try {
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = "{\"id\": \"ABCD\", \"report\": { \"data\": { \"phone\": { \"mobile_number\": 9876543210, \"active\": \"Y\", \"content\": null } } } }";
            JsonNode rootNode = mapper.readTree(jsonString);

            JsonNode mobileNumber = rootNode.path("report").path("data").path("phone").path("mobile_number");
            System.out.println("Mobile Number: " + mobileNumber.longValue());

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

So there are lot of ways to do it but everything leads eventually to traversing the tree.所以有很多方法可以做到这一点,但一切最终都会导致遍历树。

So to conclude all the approaches,所以总结所有的方法,

1. **Convert string to JsonObject and traverse.** 

    JSONObject jsonObject = new JSONObject(json);
    JSONObject report = getJSONObjectFromJson(jsonObject, "report");
    JSONObject data = getJSONObjectFromJson(jsonObject, "data");
    JSONObject phone = getJSONObjectFromJson(data, "phone");
    long mobileNumber = getLongFromJson(phone, "mobile_number");
    
    private Long getLongFromJson(JSONObject object, String key){
        return (object !=null && object.has(key)) ? object.getLong(key) : null;
    }
    
    private JSONObject getJSONObjectFromJson(JSONObject object, String key){
        return (object !=null && object.has(key)) ? object.getJSONObject(key) : null;
    }

 2. **Using jackson objectMapper to get the JsonNode and then traverse.**

    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode= mapper.readTree(json);
    JsonNode mobileNumber = jsonNode.path("report").path("data").path("phone").path("mobile_number");

 3. **Using gson jsonmapper to convert to map and then iterate the map.**

    Gson gson = new Gson();
    Map map = gson.fromJson(json, Map.class);

I encourage you to use the Jackson ObjectMapper for such task.我鼓励您使用 Jackson ObjectMapper 来完成此类任务。

Here is a tutorial explaining how to use it这是一个解释如何使用它的教程

jsonObject.getJSONObject("x").getJSONObject("Y").getJSONObject("z");

Another route would be to leverage the ObjectMapper .另一种方法是利用ObjectMapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM