【问题标题】:How can I split and print a section of JSON/String?如何拆分和打印一段 JSON/String?
【发布时间】:2019-06-06 09:38:14
【问题描述】:

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

我尝试将字符串转换回 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

【问题讨论】:

标签: java json


【解决方案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());

JsonPath 与 Jackson 或 Gson

根据您的需要,您可以将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>>() {});

【讨论】:

  • List&lt;String&gt; required = gson.fromJson(jsonObject.getAsJsonArray("required"), new TypeToken&lt;List&lt;String&gt;&gt;() {}.getType()); 这是一种荒谬的阅读方式,你解析相同的 json 两次,什么
【解决方案2】:
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);

【讨论】:

    【解决方案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"
      ]
    }
    }
    

    【讨论】:

    • 还有其他选择吗,我可以试试!
    • 我不确定你想做什么,如果你的输入相同,你总是可以手动进行。解析为 JsonObject,然后 JsonArray a = obj.get("key").get("required").getAsArray() 然后遍历所述数组
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多