gpt4 book ai didi

java - 将 JSON 文件解析为 Java 对象

转载 作者:行者123 更新时间:2023-12-01 17:17:28 25 4
gpt4 key购买 nike

下面是一个 JSON 文件,它使用包含数据的 pdf 文件表中的 OCR 并获取下面的 JSON。我想要此 JSON 中的“DATE”、“SUM”、“MULTIPLE”内容并转换为 JAVA,这样我就可以将其存储到数据库中。我听说 Jackson 或 JSON validator 可以解决这个问题,但是在这种 JSON 格式中,我该如何处理呢?例如,您会看到“content”:“DATE”,如何将“DATE”转换为JAVA对象,然后存储到数据库中?

{"metadata": [],
"pages": [
{
"margins": {
"top": -1,
"left": -1,
"bottom": -1,
"right": -1
},
"box": {
"l": 0,
"t": 0,
"w": 738,
"h": 952
},
"rotation": {
"degrees": "0",
"origin": {
"x": "369",
"y": "476"
},
"translation": {
"x": "0",
"y": "0"
}
},
"pageNumber": 1,
"elements": [
{
"id": 519,
"type": "paragraph",
"properties": {
"order": 0
},
"metadata": [],
"box": {
"l": 102.15,
"t": 118.92,
"w": 107.97,
"h": 106.68
},
"content": [
{
"id": 8,
"type": "line",
"properties": {
"order": 0,
"cr": 210.12,
"cl": 76.43
},
"metadata": [],
"box": {
"l": 168.78,
"t": 118.92,
"w": 41.35,
"h": 12.61
},
"content": [
{
"id": 7,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 168.78,
"t": 118.92,
"w": 41.35,
"h": 12.61
},
"content": "**DATE:**",
"font": 1
}
]
},
{
"id": 18,
"type": "line",
"properties": {
"order": 1,
"cr": 210.12,
"cl": 76.43
},
"metadata": [],
"box": {
"l": 102.15,
"t": 150.24,
"w": 104.5,
"h": 12.54
},
"content": [
{
"id": 16,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 102.15,
"t": 150.24,
"w": 46.49,
"h": 12.28
},
"content": "**SALES**",
"font": 1
},
{
"id": 17,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 152.34,
"t": 150.32,
"w": 54.31,
"h": 12.46
},
"content": "**ORDER:**",
"font": 1
}
]
}

最佳答案

Java 有很多 JSON 解析器:

    Gson gson=new Gson();
MyObject=gson.fromJson(jsonString, MyObject.class);
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonObject jsonObject = jsonReader.readObject();
int myInt = jsonObject.getInt("myInt"); // or getString(), getJsonObject() etc.

关于java - 将 JSON 文件解析为 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61354325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com