简体   繁体   中英

Read JSON-File with Java

I try to read to following JSON-File:

[
  {
    "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:

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. 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

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.

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. That nested object seems completely unnecessary.

Ultimately your JSON would look like this:

[
  {
    "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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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