1

I have a JSON like this one:

{
"school" : "ifsp",
"Student_per_page" : 200,
"total : 1000,
"dados": [
    {
    "matricula":203,
    "date_admission":"09-10-2016",
    "grade":7
    },
    {
    "matricula":204,
    "date_admission":"19-10-2016",
    "grade":8
    },
    {
    "matricula":205,
    "date_admission":"08-10-2016",
    "grade":5
    }
]
}

I have a JSONObject with all the data, and I need to access the grade where matricula = 204

if i do a jsontext.get("dados") , i would get

[{"matricula":203,"date_admission":"09-10-2016","grade":7},{"matricula":204, "date_admission":"19-10-2016","grade":8},{"matricula":205,"date_admission":"09-10-2016","grade":5}] 

however, I can't go further than this to get the subElements

I'm used to programming in python where I could get something like this to get what I need it

jsontext[dados][1][grade]

I have tried:

jsontext.get("dados").get(1).get("grade"), 
jsontext.get("dados").get("grade"),
jsontext.get("grade"),
jsontext.getJsonObject("dados").get(1).get("grade")

but i always got an exception, and I'm kind stuck now

here is my code:

public static JsonObject getJson(String link){
    try{
    URL url = new URL(link);
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    request.connect();
    JsonParser jp = new JsonParser();
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
    JsonObject rootobj = root.getAsJsonObject();

    return rootobj;//.toString();
    }
    catch(Exception e){}
    return null;
}

public static void main(String[] args)()
{ 
    String link = *******;
    JsonObject jsontext = getJson(link); 
    JsonElement jsondata = jsontext.get("dados");
    System.out.print(jsontext.get("dados"));
}

if there's a better approach to get the JSON data from the URL, I'm open to that as well

Raul Quinzani
  • 493
  • 1
  • 4
  • 16

0 Answers0