我如何在 java 中遍历此 JSON
How can I iterate through this JSON in java
我正在尝试遍历 Java 中的以下 Json 对象代码,我正在使用 google gson。
意图:
在我的挥杆菜单中,我将按下标有球队名称的按钮,例如:
- 巴塞罗那足球俱乐部
然后将在按下的相应球队按钮上显示每个球员的列表。
显示的玩家列表示例:
- 布拉沃、蒙托亚、皮克、拉基蒂奇等
我很难理解如何使用这个 Json 文件来做这件事,我愿意使用完成工作所需的任何库,甚至重新格式化这个 Json 文件本身。
EXTRA:如果有人还可以解释浏览数据的基础知识,以便将来我可以显示团队中每个球员的所有数据(年龄、国籍...),那也很棒!
{
"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"
}
}
}
}
这可能有助于您理解迭代
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",
}
}
我创建了一个伪代码检查一下。
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();
}
我正在尝试遍历 Java 中的以下 Json 对象代码,我正在使用 google gson。
意图: 在我的挥杆菜单中,我将按下标有球队名称的按钮,例如:
- 巴塞罗那足球俱乐部
然后将在按下的相应球队按钮上显示每个球员的列表。
显示的玩家列表示例:
- 布拉沃、蒙托亚、皮克、拉基蒂奇等
我很难理解如何使用这个 Json 文件来做这件事,我愿意使用完成工作所需的任何库,甚至重新格式化这个 Json 文件本身。
EXTRA:如果有人还可以解释浏览数据的基础知识,以便将来我可以显示团队中每个球员的所有数据(年龄、国籍...),那也很棒!
{
"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"
}
}
}
}
这可能有助于您理解迭代
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",
}
}
我创建了一个伪代码检查一下。
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();
}