HiPownedBi HiPownedBi - 10 months ago 59
Java Question


if my JSON file is like this:

{"result":[{"sentence": "Chinese government cafes people cafe crackdown", "id": 1, "txtfile": "002.txt"}, {"sentence": "kalf alo ldk alf", "id": 2, "txtfile": "003.txt"}]}

How to read .json file into java and parser the JSON to get the sentence, id, txtfile ? since my json consists both JSONARRAY AND JSONOBJECT.

private static String jsonFile="D:\\MYJSON.json";

public static void main(String[] args){

JSONParser parser = new JSONParser();


Object obj=parser.parse(new FileReader(jsonFile));
JSONObject jsonObject = (JSONObject) obj;
String sentence=(String) jsonObject.get("sentence");
} catch (Exception e){

I have the error as:java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject
at yyym.ttt.main(ttt.java:46)

This is my JSON

"{\"result\":[{\"sentence\": \"said Chinese government cafes people cafe crackdown\", \"id\": 1, \"txtfile\": \"002.txt\"}, {\"sentence\": \"kalf alo ldk alf\", \"id\": 2, \"txtfile\": \"003.txt\"}]}"

I checked my JSON file is valid.
however, When I print it out. It start with " , how to solve the problem?

the output is "{\"result\":[{\"sentence\": \"said Chinese government cafes people cafe crackdown\", \"id\": 1, \"txtfile\": \"002.txt\"}, {\"sentence\": \"kalf alo ldk alf\", \"id\": 2, \"txtfile\": \"003.txt\"}]}"
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONObject.<init>(JSONObject.java:195)
at org.json.JSONObject.<init>(JSONObject.java:319)
at yyym.ttt.main(ttt.java:26)

enter image description here

Answer Source

Result is an array, first you have to get that array, loop over that array and then get the desired json objects.

Library used in the following code is - org.json


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Example {

    public static void main(String[] args) {

        //String jsonString = "{\"result\":[{\"sentence\": \"Chinese government cafes people cafe crackdown\", \"id\": 1, \"txtfile\": \"002.txt\"}, {\"sentence\": \"kalf alo ldk alf\", \"id\": 2, \"txtfile\": \"003.txt\"}]}";

        String jsonString = readJsonFile("filePath");

        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray result = jsonObject.getJSONArray("result");
            for (int i =0; i < result.length(); i++){
                JSONObject j = result.getJSONObject(i);
                String s = j.getString("sentence");
                int id = j.getInt("id");
                String txtFile = j.getString("txtfile");
                System.out.println("Sentence is " + s);
                System.out.println("Id is " + id);
                System.out.println("text file is " + txtFile);
        } catch (JSONException e) {

    public static String readJsonFile(String filePath) {
    String jsonData = "";
    BufferedReader br = null;
    try {
        String line;
        br = new BufferedReader(new FileReader(filePath));
        while ((line = br.readLine()) != null) {
            jsonData += line + "\n";
    } catch (IOException e) {
    } finally {
        try {
            if (br != null) {
        } catch (IOException ex) {
    return jsonData;


  1. Sentence is Chinese government cafes people cafe crackdown.
  2. Id is 1
  3. text file is 002.txt
  4. Sentence is kalf alo ldk alf
  5. Id is 2
  6. text file is 003.txt