【问题标题】:How to get values from nested json in java?如何从java中的嵌套json中获取值?
【发布时间】:2018-09-05 06:19:25
【问题描述】:

我正在使用 spring boot 2.0.3 创建一个项目,在该项目中我必须解析 json 并使用其键获取值。我的问题是我有一个存在于 json 中多个级别的键,我想获取它所有出现的值。例如下面是我正在使用的对象:-

{
  "test": {
    id:1,
    name:"test1",
    class:"test2"
  },
  "boolean": true,
  "color": "#82b92c",
  "null": null,
  "number": 123,
  "object_": {
    "a": "b",
    "c": "d",
    "e": "f",
    "test": {
       id:2,
       name:"test1",
       class:"test2"
    }
  },
  "object": {
    "object": {
       "test": {
          id:3,
          name:"test1",
          class:"test2"
        }
    },
    "test": {
        id:4,
        name:"test1",
        class:"test2"
    }
  },
  "string": "Hello World"
}

test 出现了四次,现在我想获取它的所有值。 请建议我如何获得它的值。

【问题讨论】:

    标签: java json spring-boot


    【解决方案1】:

    如果您使用 Java DTO 类作为控制器方法参数(以便 Spring Boot 自动进行转换),您将拥有嵌套 JSON 对象的嵌套 DTO。

    在你的情况下是这样的:

    public class TestDto { 
        int id;
        String name;
        String clazz; //can't use class as it is reserved keyword
    }
    
    public class Body {
        TestDto test;
    }
    

    顺便说一句,我不认为几个 test 键是有效的 JSON。

    【讨论】:

      【解决方案2】:

      希望下面的递归逻辑有助于解析Json。

      public static void main(String[] args) {
      
          JacksonJsonParser parser= (JacksonJsonParser) JsonParserFactory.getJsonParser();
          Map jsonMap=parser.parseMap("{\"test\":{\"id\":1,\"name\":\"test1\",\"class\":\"test2\"},\"boolean\":true,\"color\":\"#82b92c\",\"null\":null,\"number\":123,\"object_\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\",\"test\":{\"id\":2,\"name\":\"test1\",\"class\":\"test2\"}},\"object\":{\"object\":{\"test\":{\"id\":3,\"name\":\"test1\",\"class\":\"test2\"}},\"test\":{\"id\":4,\"name\":\"test1\",\"class\":\"test2\"}},\"string\":\"Hello World\"}");
          getTestDetails(jsonMap);
      }
      
      private static void getTestDetails(Map jsonMap) {
          for(Object key:jsonMap.keySet()){
      
              if("test".equals(key.toString())){
                  System.out.println(jsonMap.get(key));
              }else{
      
                  if(jsonMap.get(key) instanceof Map){
                      getTestDetails((Map)jsonMap.get(key));
                  }
              }
      
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用 org json 库从字符串中读取 JSON。

        https://mvnrepository.com/artifact/org.json/json/20140107

        一旦你把罐子放好。

        下面的代码应该打印所有带有“test”键的jsons

        public static void main(String[] args) {
            String jsonString = "{\"test\": {\"id\":1,\"name\":\"test1\",\"class\":\"test2\"},\"boolean\": true,\"color\": \"#82b92c\",\"null\": null,\"number\": 123,\"object_\": {\"a\": \"b\",\"c\": \"d\",\"e\": \"f\",   \"test\": {   \"id\":2,    \"name\":\"test1\",    \"class\":\"test2\"  }  },  \"object\": {    \"object\": {       \"test\": {    \"id\":3,    \"name\":\"test1\",    \"class\":\"test2\"  }    },    \"test\": {    \"id\":4,    \"name\":\"test1\",    \"class\":\"test2\"  }  },  \"string\": \"Hello World\"}";
            JSONObject json = new JSONObject(jsonString);
            System.out.println(json);
            Iterator keys = json.keys();
            getTestKeyObj(json);
        }
        
        public static void getTestKeyObj(JSONObject jsonObj) {
            Iterator keys = jsonObj.keys();
            while( keys.hasNext() ) {
                String key = (String)keys.next();
                if(key.equalsIgnoreCase("test")) {
                    System.out.println("Test JSON: "+jsonObj.get(key));
                }else {
                    if ( jsonObj.get(key) instanceof JSONObject ) {
                        getTestKeyObj((JSONObject) jsonObj.get(key));
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-19
          • 1970-01-01
          • 2021-08-26
          • 1970-01-01
          • 2015-10-13
          • 1970-01-01
          相关资源
          最近更新 更多