Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from JSON array

Tags:

java

json

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.

like image 526
user2855405 Avatar asked Nov 18 '13 21:11

user2855405


People also ask

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

Can you JSON parse an array?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

How extract specific data from JSON Python?

So first thing you need to import the 'json' module into the file. Then create a simple json object string in python and assign it to a variable. Now we will use the loads() function from 'json' module to load the json data from the variable. We store the json data as a string in python with quotes notation.

How do I query JSON data in SQL?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).


2 Answers

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)

like image 99
kiruwka Avatar answered Oct 12 '22 14:10

kiruwka


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.

like image 44
Codo Avatar answered Oct 12 '22 14:10

Codo