【问题标题】:Parsing JSON file to Java Object将 JSON 文件解析为 Java 对象
【发布时间】:2020-04-21 22:31:25
【问题描述】:

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

{"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 parsing


【解决方案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.

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多