0

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

2
  • To make it easier for others to answer your question, you could add the relevant parts of the exception stacktrace. Commented Apr 12, 2014 at 20:11
  • I am sorry thank you for help Commented Apr 17, 2014 at 15:09

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.