Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String JSonString = readURL("//my URL is here");
- JSONArray s = JSONArray.fromObject(JSonString);
- JSONObject Data =(JSONObject)(s.getJSONObject(0));
- System.out.println(Data.get("name"));
- {
- "sports": [
- {
- "name": "basketball",
- "id": 40,
- "uid": "s:40",
- "leagues": [
- {
- "name": "National Basketball Assoc.",
- "abbreviation": "nba",
- "id": 46,
- "uid": "s:40~l:46",
- "groupId": 7,
- "shortName": "NBA",
- "athletes": []
- }
- ]
- }
- ],
- "resultsOffset": 10,
- "resultsLimit": 10,
- "resultsCount": 1,
- "timestamp": "2013-11-18T03:15:43Z",
- "status": "success"
- }
- JSONObject root = new JSONObject(yourJsonString);
- JSONArray sportsArray = root.getJSONArray("sport");
- // now get the first element:
- JSONObject firstSport = sportsArray.getJSONObject(0);
- // and so on
- String name = firstSport.getString("name"); // basketball
- int id = firstSport.getInt("id"); // 40
- JSONArray leaguesArray = firstSport.getJSONArray("leagues");
- // and so on, you can process leaguesArrays similarily
- String jsonString = readURL("//my URL is here");
- JSONObject result = JSONObject(jsonString);
- JSONArray sports = result.getJSONArray("sports");
- JSONObject sport = sport.getJSONObject(0);
- System.out.println(sport.getString("name"));
- {"name" : "Robin", "rollnumber" : "1"}
- [{"name" : "Robin", "rollnumber" : "1"}, {"name" : "Mark", "rollnumber" : "2"}]
- import org.json.JSONException;
- import org.json.JSONObject;
- public class extractingJSON {
- public static void main(String[] args) throws JSONException {
- // TODO Auto-generated method stub
- String jsonStr = "{"name":"SK","arr":{"a":"1","b":"2"},"arrArray":[{"a":"1","b":"2"}]}";
- JSONObject jsonObj = new JSONObject(jsonStr);
- String name = jsonObj.getString("name");
- System.out.println(name);
- String first = jsonObj.getJSONObject("arr").getString("a");
- System.out.println(first);
- first = jsonObj.getJSONArray("arrArray").getJSONObject(0).getString("a");
- System.out.println(first);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement