-3

Edited. Anyone know how to read the record in every new line. The below code can only read 1 sentence.

JSONParser parser = new JSONParser();
    try
    {
        Object obj = parser.parse(new FileReader("src/test.json"));
        JSONObject jsonObject = (JSONObject) obj;
        String name = (String) jsonObject.get("name");
        System.out.println(name);



    }
  • 3
    http://stackoverflow.com/questions/8939250/parsing-json-file-java – Colonel Panic Jun 18 '13 at 04:02
  • You can use any JSON parsing libs like [Jackson](http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/). – Santosh Jun 18 '13 at 04:02
  • I followed the download here link and it is a zip file and add external library to eclipse but still have error after inserting import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; – user2483015 Jun 18 '13 at 04:33
  • please post your json file, thing is once you have got the jsonobject then you need to iterate/fetch from the object based on your json structure. – Sikorski Jun 18 '13 at 14:50

2 Answers2

1

Many ways. I suggest you use google-gson

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0

You can use codehouse jackson

ObjectMapper mapper = new ObjectMapper();
BufferedReader br = new BufferedReader(new FileReader(tweets));
Map<String, Object> json_as_map = mapper.readValue(br, new TypeReference<Map<String, Object>>() {});

After that you can traverse your map or arraylist according to ur structure.

Anurag Tripathi
  • 1,208
  • 1
  • 12
  • 31
  • You have to add http://search.maven.org/remotecontent?filepath=org/codehaus/jackson/jackson-jaxrs/1.9.12/jackson-jaxrs-1.9.12.jar in your classpath. And then use JAVA Collection APIs to parse JSON. – Anurag Tripathi Jun 19 '13 at 07:12