我有一个像这样的JSON:
{
"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
}
]
}
我有一个包含所有数据的JSONObject,我需要访问矩阵= 204的等级
如果我做一个 jsontext.get(“ dados”) ,我会得到
[{"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}]
但是,除了获得子元素,我只能走这步
我习惯于用python编程,在那里我可以得到类似这样的东西来获得我需要的东西
jsontext[dados][1][grade]
我尝试过:
jsontext.get("dados").get(1).get("grade"),
jsontext.get("dados").get("grade"),
jsontext.get("grade"),
jsontext.getJsonObject("dados").get(1).get("grade")
但是我总是有一个例外,现在我很困住
这是我的代码:
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"));
}
如果有更好的方法从URL获取JSON数据,我也很乐意