save the data inside [] as [{"id_user":"10","level":"medium","text":"hello 10"} {"id_user":"20","level":"medium","text":"hello 20"} {"id_user":"30","level":"medium","text":"hello 30"}]
inside the file so that it becomes a list, then you can use JSONArray. what i wrote is as follows
public class JSONReadFromFile { @SuppressWarnings("unchecked") public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader("\\D:\\JSON\\file3.txt")); JSONArray jsonArray = (JSONArray) obj; int length = jsonArray.size(); LinkedList name = new LinkedList(); LinkedList author = new LinkedList(); LinkedList company = new LinkedList(); for (int i =0; i< length; i++) { JSONObject jsonObject = (JSONObject) jsonArray.get(i); Set s = jsonObject.entrySet(); Iterator iter = s.iterator(); LinkedList ll = new LinkedList(); LinkedList lm = new LinkedList(); while(iter.hasNext()){ Map.Entry me = (Map.Entry) iter.next();
}
The output I get is
[10, 20, 30] [medium, medium, medium] [hello 10, hello 20, hello 30]
if you uncomment "System.out.println (me.getKey () +" "+ me.getValue ());" you get the following output level medium id_user 10 text hello 10 level medium id_user 20 text hello 20 level medium id_user 30 text hello 30
I like the first one (three linked lists), these lists are ordered and can be easily edited / searched, I mean using the index, I can always find the corresponding items from other lists.
AMITAVA
source share