Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. String JSonString = readURL("//my URL is here");
  2. JSONArray s = JSONArray.fromObject(JSonString);
  3. JSONObject Data =(JSONObject)(s.getJSONObject(0));
  4. System.out.println(Data.get("name"));
  5.  
  6. {
  7. "sports": [
  8. {
  9. "name": "basketball",
  10. "id": 40,
  11. "uid": "s:40",
  12. "leagues": [
  13. {
  14. "name": "National Basketball Assoc.",
  15. "abbreviation": "nba",
  16. "id": 46,
  17. "uid": "s:40~l:46",
  18. "groupId": 7,
  19. "shortName": "NBA",
  20. "athletes": []
  21. }
  22. ]
  23. }
  24. ],
  25. "resultsOffset": 10,
  26. "resultsLimit": 10,
  27. "resultsCount": 1,
  28. "timestamp": "2013-11-18T03:15:43Z",
  29. "status": "success"
  30. }
  31.  
  32. JSONObject root = new JSONObject(yourJsonString);
  33. JSONArray sportsArray = root.getJSONArray("sport");
  34. // now get the first element:
  35. JSONObject firstSport = sportsArray.getJSONObject(0);
  36. // and so on
  37. String name = firstSport.getString("name"); // basketball
  38. int id = firstSport.getInt("id"); // 40
  39. JSONArray leaguesArray = firstSport.getJSONArray("leagues");
  40.  
  41. // and so on, you can process leaguesArrays similarily
  42.  
  43. String jsonString = readURL("//my URL is here");
  44. JSONObject result = JSONObject(jsonString);
  45. JSONArray sports = result.getJSONArray("sports");
  46. JSONObject sport = sport.getJSONObject(0);
  47. System.out.println(sport.getString("name"));
  48.  
  49. {"name" : "Robin", "rollnumber" : "1"}
  50.  
  51. [{"name" : "Robin", "rollnumber" : "1"}, {"name" : "Mark", "rollnumber" : "2"}]
  52.  
  53. import org.json.JSONException;
  54. import org.json.JSONObject;
  55.  
  56. public class extractingJSON {
  57.  
  58. public static void main(String[] args) throws JSONException {
  59. // TODO Auto-generated method stub
  60.  
  61. String jsonStr = "{"name":"SK","arr":{"a":"1","b":"2"},"arrArray":[{"a":"1","b":"2"}]}";
  62. JSONObject jsonObj = new JSONObject(jsonStr);
  63. String name = jsonObj.getString("name");
  64. System.out.println(name);
  65.  
  66. String first = jsonObj.getJSONObject("arr").getString("a");
  67. System.out.println(first);
  68.  
  69. first = jsonObj.getJSONArray("arrArray").getJSONObject(0).getString("a");
  70. System.out.println(first);
  71.  
  72.  
  73.  
  74. }
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement