Guest User

Untitled

a guest
Mar 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. '[{ "var1": 30, "city1": {"tempC": 1, "tempF": 5}, "city2": {"tempC": 2, "tempF": 6}, "city3": {"tempC": 3, "tempF": 7}, "city4": {"tempC": 4, "tempF": 8}, "var2": 5}]
  2.  
  3. String custom = "(30,1,2,3,4,5,6,7,8,5)";
  4. custom = custom.substring(1,custom.length()-1);
  5.  
  6. JSONObject jsonSubObject = null;
  7. JSONObject jsonFinal = new JSONObject();
  8. JSONArray jsonArrayRET = new JSONArray();
  9.  
  10.  
  11. String []array2 = custom.split(",");
  12. String []cityArray = Arrays.copyOfRange(array2, 1, 13);
  13.  
  14. // delete the elements from array2 that have been copied into cityArray
  15. for(int i = 0; i < 12; i++) {
  16. array2 = (String[]) ArrayUtils.remove(array2, 1);
  17. }
  18.  
  19. //I need to make the number of cities dynamic that would depend on the number of elements in half an array and hence I need to create a dynamic city variable at every run
  20. int numCities = ((Arrays.copyOfRange(cityArray, 0, (cityArray.length + 1)/2)).length);
  21. for(int i = 0; i < numCities; i++){
  22. ArrayList<String> city[i] = new ArrayList<String>();
  23. city[i].add(cityArray[i]);
  24. city[i].add(cityArray[i+numCities]);
  25. }
  26.  
  27. jsonSubObject = new JSONObject();
  28. jsonSubObject.put("var1", array2[0]);
  29. jsonSubObject.put("var2", array2[custom.length()-1]);
  30. //Here I need to add city[1], city[2],city[3] anmd city[4]
  31. jsonArrayRET.add(jsonSubObject);
  32.  
  33. System.out.println(jsonArrayRET);
Add Comment
Please, Sign In to add comment