从JSON数组中提取数据 [英] Extracting data from JSON array

查看:708
本文介绍了从JSON数组中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它是一个数组,但我是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"));

我的JSON数据是这样的:

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.

推荐答案

以下是这个想法:

JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sport");
// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);
// and so on
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");

// and so on, you can process leaguesArrays similarily

它应该工作(如果有的话,随时抱怨编译错误)

It should work (feel free to complain about compile errors if there are any)

这篇关于从JSON数组中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆