Marcel
Marcel

Reputation: 3

How can read JSON with multiple things in sigle file java

I work with JSON an i came across a problem. I have JSON file like this

{"Text":"Here is some text","Make":"Admin","Name":"Hello"}
{"Text":"Here is some text","Make":"John","Name":"Hello"}
{"Text":"Here is some text","Make":"Admin","Name":"Hello"}

and I need to read from this file all text. I try but throws exception here is my code to read

JSONParser parser = new JSONParser();
     Object obj = null;
     try {
             obj = parser.parse(new FileReader("Project.json"));

         JSONObject jsonObject = (JSONObject) obj;
     String name = (String) jsonObject.get("Text");
     System.out.println(name);

     }
     catch (IOException e) {
         e.printStackTrace();
     } catch (org.json.simple.parser.ParseException e) {
         e.printStackTrace();
     }

Thx for help

Upvotes: 0

Views: 70

Answers (1)

Faisal Ali
Faisal Ali

Reputation: 1135

This is not valid JSON, It should be like this

[{"Text":"Here is some text","Make":"Admin","Name":"Hello"},
{"Text":"Here is some text","Make":"John","Name":"Hello"},
{"Text":"Here is some text","Make":"Admin","Name":"Hello"}]

obj = parser.parse(new FileReader("Project.json"));
JSONArray jsonArray = (JSONArray) obj;

for (JSONObject jsonObject : jsonArray)
{
    String name = (String) jsonObject.get("Text");
    System.out.println(name);
}

Hopefully this will solve your problem

Upvotes: 1

Related Questions