简体   繁体   English

使用 Java 读取 JSON 文件

[英]Read JSON-File with Java

I try to read to following JSON-File:我尝试阅读以下 JSON 文件:

[
  {
    "0": {
      "owner_ip": 0,
      "id": 0,
      "text": "test",
      "timestamp": ""
    }
  },
  {
    "1": {
      "owner_ip": 0,
      "id": 1,
      "text": "test",
      "timestamp": ""
    }
  },
  {
    "2": {
      "owner_ip": 0,
      "id": 2,
      "text": "test",
      "timestamp": ""
    }
  },
  {
    "3": {
      "owner_ip": 0,
      "id": 3,
      "text": "test",
      "timestamp": ""
    }
  },
  {
    "4": {
      "owner_ip": 0,
      "id": 4,
      "text": "test",
      "timestamp": ""
    }
  }
]

I imported the following package's:我导入了以下包:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

I want to get the value of "0"->"text", I execute the following operation:我想获取“0”->“text”的值,我执行如下操作:

JSONParser jsonParser = new JSONParser();

try(FileReader reader = new FileReader("filename"){
    Object obj = jsonParser.parse(reader);
    JSONArray readList = (JSONArray) obj;
    System.out.println(readList);
    System.out.println(readList.get(0));
    System.out.println(readList.get(0).getClass().getName());
    // Output = {"0":{"owner_ip":0,"id":0,"text":"test","timestamp":""}}
    // Output = org.json.simple.JSONObject

After this I don't know how to code further.在此之后,我不知道如何进一步编码。 I try a lot of different ways, but nothing works.我尝试了很多不同的方法,但没有任何效果。 I hope you can help me.我希望你能帮助我。

Your JSON is oddly formatted.您的 JSON 格式奇怪。 It has an array of objects and those objects have one entry with the same key as the index they have in the loop.它有一个对象数组,这些对象有一个条目,其键与它们在循环中的索引相同。

The JSON path of the text entry of the first element would be第一个元素的text条目的 JSON 路径将是

json[0]["0"].text

You see 0 twice because you need to first select the first element of the array and then the object with key "0" within that array.您会看到0两次,因为您需要首先 select 是数组的第一个元素,然后是 object 在该数组中的键为"0"

In your code you can do it like this:在您的代码中,您可以这样做:

JSONArray array = (JSONArray) jsonParser.parse(reader);
JSONObject obj = (JSONObject) array.get(0);
JSONObject nestedObj = (JSONObject) obj.get("0");
String textValue = (String) nestedObj.get("text");
System.out.println(textValue);

Though I would recommend to fix the format of your JSON.虽然我建议修复 JSON 的格式。 That nested object seems completely unnecessary.嵌套的 object 似乎完全没有必要。

Ultimately your JSON would look like this:最终,您的 JSON 将如下所示:

[
  {
    "owner_ip": 0,
    "id": 0,
    "text": "test",
    "timestamp": ""
  },
  {
    "owner_ip": 0,
    "id": 1,
    "text": "test",
    "timestamp": ""
  },
  {
    "owner_ip": 0,
    "id": 2,
    "text": "test",
    "timestamp": ""
  },
  {
    "owner_ip": 0,
    "id": 3,
    "text": "test",
    "timestamp": ""
  },
  {
    "owner_ip": 0,
    "id": 4,
    "text": "test",
    "timestamp": ""
  }
]

Then you can skip the obj.get("0") line in the code.然后你可以跳过代码中的obj.get("0")行。

JSONArray array = (JSONArray) jsonParser.parse(reader);
JSONObject obj = (JSONObject) array.get(0);
String textValue = (String) obj.get("text");
System.out.println(textValue);

If you are sure that the structure remains the same,如果您确定结构保持不变,

JSONArray readList = (JSONArray) obj;
JSONObject targetObj = readList.get(0);
System.out.println(targetObj.get("text")); // will print "test"

Or you can iterate throw the JSONArray.或者您可以迭代抛出 JSONArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM