我怎样才能在java中迭代这个JSON

时间:2015-03-12 04:12:23

标签: java json iteration

我试图在Java中迭代以下JsonObject代码,我正在使用google gson。

意图: 在我的挥杆菜单中,我将按下标有团队名称的按钮,例如:

  • 巴塞罗那足球俱乐部

然后,将显示按下的相应团队按钮上的每个玩家的列表。

显示的播放器列表示例:

  • Bravo,Montoya,Pique,Rakitic,.etc

我很难理解如何使用这个Json文件来做这件事,我愿意使用任何完成工作所需的库,甚至重新格式化这个Json文件本身。

EXTRA:如果有人也可以解释导航数据的基础知识,以便将来我可以显示团队中每个玩家的所有数据(年龄,国籍......),这也很棒! / p>



{
	"Teams":
		{
			"FC Barcelona":{
				"Bravo":{
					"age" : "32",
					"nationality" : "Chile",
					"club" : "FC Barcelona",
					"position": "Goalkeeper",
					"overall" : "83"
				},
				"Montoya":{
					"age" : "24",
					"nationality" : "Spain",
					"club" : "FC Barcelona",
					"position" : "Defender",
					"overall" : "77"
				},
				"Pique":{
					"age" : "28",
					"nationality" : "Spain",
					"club" : "Barcelona",
					"position" : "Defender",
					"overall" : "84"
				},
				"Rakitic":{
					"age" : "27",
					"nationality" : "Croatia",
					"club" : "Barcelona",
					"position" : "Midfielder",
					"overall" : "83"
				},
				"Busquets":{
					"age" : "27",
					"nationality" : "Spain",
					"club" : "Barcelona",
					"position" : "Midfielder",
					"overall" : "86"
				},
				"Xavi":{
					"age" : "35",
					"nationality" : "Spain",
					"club" : "Barcelona",
					"position" : "Midfielder",
					"overall" : "86"
				},
				"Iniesta":{
					"age" : "31",
					"nationality" : "Spain",
					"club" : "Barcelona",
					"position" : "Midfielder",
					"overall" : ""
				},
				"Pedro":{
					"age" : "28",
					"nationality" : "Spain",
					"club" : "Barcelona",
					"position" : "Forward",
					"overall" : "83"
				},	
				"Suarez":{
					"age" : "28",
					"nationality" : "Uruguay",
					"club" : "Barcelona",
					"position" : "Forward",
					"overall" : "89"
				},				
				"Messi":{
					"age" : "28",
					"nationality" : "Argentina",
					"club" : "Barcelona",
					"position" : "Forward",
					"overall" : "93"
				},
				"Neymar":{
					"age" : "23",
					"nationality" : "Brazil",
					"club" : "Barcelona",
					"position" : "Forward",
					"overall" : "87"
				}				
			}
		}
}




2 个答案:

答案 0 :(得分:0)

这可以帮助您理解迭代

public class JsonParseTest {

    private static final String filePath = "E:\\testJson.json";

    public static void main(String[] args) {

        try {

            // read the json file

            FileReader reader = new FileReader(filePath);



            JSONParser jsonParser = new JSONParser();

            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
         // get a String from the JSON object

            String firstName = (String) jsonObject.get("firstname");

            System.out.println("The first name is: " + firstName);


          // get a number from the JSON object

            long id =  (long) jsonObject.get("id");

            System.out.println("The id is: " + id);



            // get an array from the JSON object

          JSONArray lang= (JSONArray) jsonObject.get("languages");



            // take the elements of the json array

            for(int i=0; i<lang.size(); i++){

                System.out.println("The " + i + " element of the array: "+lang.get(i));

            }

            Iterator i = lang.iterator();



            // take each value from the json array separately

            while (i.hasNext()) {

                JSONObject innerObj = (JSONObject) i.next();

                System.out.println("language "+ innerObj.get("lang") +

                        " with level " + innerObj.get("knowledge"));

            }

            // handle a structure into the json object

            JSONObject structure = (JSONObject) jsonObject.get("job");

            System.out.println("Into job structure, name: " + structure.get("name"));



        } catch (FileNotFoundException ex) {

            ex.printStackTrace();

        } catch (IOException ex) {

            ex.printStackTrace();

        } catch (ParseException ex) {

            ex.printStackTrace();

        } catch (NullPointerException ex) {

            ex.printStackTrace();

        }



  }




 }

这是json文件

{
    "id": 1,
        "firstname": "Katerina",
    "languages": [
        { "lang":"en" , "knowledge":"proficient" }, 
        { "lang":"fr" , "knowledge":"advanced" }, 
    ]
    "job":{
                "site":"www.javacodegeeks.com",
                "name":"Java Code Geeks",
        }  
}

答案 1 :(得分:0)

我创建了一个伪代码检查它。

    try {
                JSONObject root = new JSONObject("YOUR_JSON");
                JSONObject team = root.getJSONObject("Teams").getJSONObject("FC Barcelona");
                Iterator keys =  team.keys(); 

                //iterate each object
                while (keys.hasNext()){
                    JSONObject obj = team.getJSONObject((String)keys.next());
                    String age = obj.getString("age");
                }


            } catch (JSONException e) {
                e.printStackTrace();
            }