简体   繁体   English

如何在Java中遍历此JSON

[英]How can I iterate through this JSON in java

I'm trying to iterate through the following JsonObject code in Java , I am using the google gson. 我正在尝试遍历Java中的以下JsonObject代码,而我正在使用google gson。

Intent: In my swing menu, I will press the button labelled with the team name, example: 目的:在挥杆菜单中,我将按下标有团队名称的按钮,例如:

  • FC Barcelona 巴塞罗那足球俱乐部

That will then display a list of each player on the respective team button that is pressed. 然后将在所按下的各个团队按钮上显示每个球员的列表。

Example displayed player list: 示例显示的播放器列表:

  • Bravo, Montoya, Pique, Rakitic , .etc Bravo,Montoya,Pique,Rakitic等

I am having a hard time understanding how I can go about doing this using this Json file, I'm willing to use any library needed to get the job done, or even reformatting this Json file itself. 我很难理解如何使用此Json文件执行此操作,我愿意使用完成工作所需的任何库,甚至重新格式化此Json文件本身。

EXTRA: If anyone could also explain the basics of navigating through the data so that in the future I can display all the data (age,nationality...) for each player in a team, that would also be great! 附加:如果有人还能解释浏览数据的基础知识,以便将来我可以显示团队中每个球员的所有数据(年龄,国籍...),那也很棒!

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

This may help you to understand iterating 这可以帮助您了解迭代

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();

        }



  }




 }

This is the json file 这是json文件

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

I have created one pseudo code check it. 我创建了一个伪代码检查它。

    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();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM