13

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.

13
  • 1
    You have to extract sports first. Commented Nov 18, 2013 at 21:24
  • like I said, Im clueless as to how this is structured so code examples would help. Commented Nov 18, 2013 at 21:27
  • 1
    The JSON data in your question is an object (and not an array) and it's invalid. (Try jsonlint.com or a similar tool.) Commented Nov 18, 2013 at 21:27
  • then what do the brackets and curly braces represent? Commented Nov 18, 2013 at 21:30
  • 1
    An opening curly brace starts a JSON object. An opening bracket starts an array. Your data starts with a curly brace (object) and ends with a closing bracket (invalid). Commented Nov 18, 2013 at 21:32

4 Answers 4

15

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for help 1 decade back, it helped me to solve a problem today :) stackoverflow.com/questions/75519681/…
3

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.

Comments

0

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: E.g. Student

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

Represent array in json : E.g. 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

Comments

0

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



    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.