Query a JSONObject in java

后端 未结 6 1724
鱼传尺愫
鱼传尺愫 2020-11-27 06:12

I was wondering if somewhere out there exists a java library able to query a JSONObject. In more depth I\'m looking for something like:



        
6条回答
  •  天命终不由人
    2020-11-27 07:17

    First of all, I would recommend consider JSON object binding.

    But in case if you get arbitrary JSON objects and you would like process them in the way you described, I would suggest combine Jackson JSON processor along with Apache's Commons Beanutils.

    The idea is the following: Jackson by default process all JSON's as java.util.Map instances, meanwhile Commons Beanutils simplifies property access for objects, including arrays and Map supports.

    So you may use it something like this:

    //actually it is a Map instance with maps-fields within
    Object jsonObj = objectMapper.readValue(json, Object.class);
    
    Object hello = PropertyUtils.getProperty(jsonObj, "data.data2.value")
    
    System.out.println(hello); //prints hello
    

提交回复
热议问题