从JSON数组中提取数据

我知道它是一个数组,但是我对JSON完全陌生,需要帮助理解它的结构,这是我提取数据的尝试:

String JSonString = readURL("//my URL is here");

JSONArray s = JSONArray.fromObject(JSonString);

JSONObject Data =(JSONObject)(s.getJSONObject(0));

System.out.println(Data.get("name"));

我拥有的JSON数据如下所示:

 {

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

}

我对这些东西的掌握并不强,因此感谢所有帮助。

回答:

这是个主意:

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

它应该可以工作(如果有编译错误,请随时投诉)

以上是 从JSON数组中提取数据 的全部内容, 来源链接: utcz.com/qa/425398.html

回到顶部