Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. String string=new String("[{\"id\":\"2018\",\"ceaNumber\":\"R000001X\",\"fullName\":\"Teszt 1\"},{\"id\":\"2019\",\"ceaNumber\":\"R000002X\",\"fullName\":\"Teszt 2\"}]");
  2.         try {
  3.             JSONArray jsonArr = new JSONArray(string);
  4.             HashMap<String,User> users = new HashMap<String,User>();
  5.             for (int i = 0; i < jsonArr.length(); i++)
  6.             {
  7.                 JSONObject jsonObj = jsonArr.getJSONObject(i);
  8.                 Iterator iterator = jsonObj.keys();
  9.  
  10.                 String id = null;
  11.                 User u=new User();
  12.                 while(iterator.hasNext()){
  13.                     String key = (String) iterator.next();
  14.                     if(key.equals("id")){
  15.                         id=(String) jsonObj.get(key);
  16.                         u.id=id;
  17.                     }
  18.                     if(key.equals("ceaNumber")){
  19.                         u.ceaNumber=(String)jsonObj.get(key);
  20.                     }
  21.                     if(key.equals("fullName")){
  22.                         u.fullName=(String)jsonObj.get(key);
  23.                     }
  24.                 }
  25.                 users.put(id,u);
  26.             }
  27.             System.out.println(users);
  28.         } catch (JSONException e) {
  29.             e.printStackTrace();
  30.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement