有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何拆分和打印JSON/String的一部分?

我有一个从数据库中获取的JSON模式,它现在存储在Java中的字符串中。我只想打印模式的一部分,而不是全部。如何拆分JSON/字符串并打印

我尝试将字符串转换回JSON格式。但不确定如何分离所需的内容。同样,分割法对我也不起作用

输入:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "employee_id": {
      "type": "string"
    },
    "course_id": {
      "type": "string"
    },
    "college_id": {
      "type": "string"
    }
  },
  "required": [
    "employee_id",
    "course_id",
    "college_id"
  ]
}

预期结果:

employee_id, course_id, college_id

共 (4) 个答案

  1. # 1 楼答案

    由于您的问题没有提供有关您正在使用哪个库来解析JSON文档的任何详细信息,因此我总结了一些使用流行的Java JSON解析库的方法

    JsonPath

    JsonPath实现这一点非常简单:

    List<String> required = JsonPath.parse(json).read("$.required");
    

    杰克逊

    这也可以通过Jackson实现:

    ObjectMapper mapper = new ObjectMapper();
    List<String> required = mapper.convertValue(mapper.readTree(json).get("required"),
            new TypeReference<List<String>>() {});
    

    格森

    如果您更喜欢Gson

    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
    List<String> required = gson.fromJson(jsonObject.getAsJsonArray("required"),
            new TypeToken<List<String>>() {}.getType());
    

    带Jackson或Gson的JsonPath

    根据您的需要,您可以将JsonPathJacksonGson结合使用:

    Configuration conf = Configuration.builder()
            .jsonProvider(new JacksonJsonProvider())
            .mappingProvider(new JacksonMappingProvider())
            .build();
    
    Configuration conf = Configuration.builder()
            .jsonProvider(new GsonJsonProvider())
            .mappingProvider(new GsonMappingProvider())
            .build();
    
    List<String> required = JsonPath
            .using(conf)
            .parse(json)
            .read("$.required", new TypeRef<List<String>>() {});
    
  2. # 2 楼答案

    下面提到的方法解决了我的问题

            JSONObject jsonObject = new JSONObject(key);
            JSONArray req = jsonObject.getJSONArray("required");
            System.out.println("Required Parameters : "+req);
    
  3. # 3 楼答案

    我制作了一个使用gson的助手库,它能够在json中搜索子树/元素:

    https://github.com/Enerccio/gson-utilities

    在你的情况下,你会这样做

    List<JsonElement> contents = JsonHelper.getAll(data, "key.required", x -> x instanceof JsonPrimitive);
    System.out.print(contents.stream().map(JsonElement::getAsString).collect(Collectors.joining(", ")));
    

    但您的JSON无效,有效版本为:

    {
    "key":{
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "employee_id": {
          "type": "string"
        },
        "course_id": {
          "type": "string"
        },
        "college_id": {
          "type": "string"
        }
      },
      "required": [
        "employee_id",
        "course_id",
        "college_id"
      ]
    }
    }
    
  4. # 4 楼答案

    String str=
     "{
    "key":{
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "employee_id": {
          "type": "string"
        },
        "course_id": {
          "type": "string"
        },
        "college_id": {
          "type": "string"
        }
      },
      "required": [
        "employee_id",
        "course_id",
        "college_id"
      ]
    }
    }
    ";  
    
    
    
            JSONObject jsonObj=new JSONObject(str);
           JSONObject keyJon= jsonObj.getJSONObject("key");
           String strUrl=keyJon.getString("$schema");
           System.err.println("str  "+strUrl);