简体   繁体   中英

Extracting data from JSON array

I know its an array, but I am completely new to JSON and need help comprehending how this is structured, here is my attempt at extracting data:

String JSonString = readURL("//my URL is here");
JSONArray s = JSONArray.fromObject(JSonString);
JSONObject Data =(JSONObject)(s.getJSONObject(0));
System.out.println(Data.get("name"));

My JSON data that I have goes like this :

 {
"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"
}

I dont really have a strong grasp of this stuff so all the help is appreciated.

Here is the idea :

JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sports");

// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);

// and details of the first element
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");

// and so on, you can process leaguesArray similarly

It should work (feel free to complain about compile errors if there are any)

Your JSON data is an object (it starts with a curly brace). In the next inner layer, there is a single array (at key "sports"):

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"));

I might have used another JSON library than you.

JSON means JavaScript Object Notation.

Objects in javascripts are just containers and can be represented by key-value pairs. Please find below notations to understand about json.

Represent objects in json: Eg Student

{"name" : "Robin", "rollnumber" : "1"}

Represent array in json : Eg Array of students

[{"name" : "Robin", "rollnumber" : "1"}, {"name" : "Mark", "rollnumber" : "2"}]

You can understand more on JSON from diagrams on this link http://www.json.org/fatfree.html

There are various ways available to to convert JSON to javaobject and javaobject to JSON : One of them is http://wiki.fasterxml.com/JacksonInFiveMinutes

Adding detailed code here along with the imports .

If this helps.

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);



    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM