我在处理(v.3.0.1)以读取JSON文件时遇到问题,我收回了标题上的错误。
这是JSON文件的一部分(减去几百个类似于我展示的对象):
{
"matches":[
{
"p1":{
"reds":0,
"team":"Liverpool",
"goals":1
},
"p2":{
"reds":0,
"team":"Bayern Munich",
"goals":2
},
"match":1
},
{
"p1":{
"reds":0,
"team":"Psg",
"goals":3
},
"p2":{
"reds":1,
"team":"Manchester City",
"goals":0
},
"match":2
}
]
}
这是Processing sketch,我尝试读取JSON文件:
String filename = "data.json";
JSONObject json;
Match[] matches;
Player[] allPlayers;
void setup(){
loadData();
size(600,300);
}
void draw(){
background(40);
}
void loadData(){
json = loadJSONObject(filename); // This is where the error happens
JSONArray matchesData = json.getJSONArray("matches");
matches= new Match[matchesData.size()];
for(int i=0; i<matchesData.size(); i++){
JSONObject match = matchesData.getJSONObject(i);
JSONObject p1 = matchesData.getJSONObject(0);
String p1Team = p1.getString("team");
int p1Goals = p1.getInt("goals");
int p1Reds = p1.getInt("reds");
JSONObject p2 = matchesData.getJSONObject(1);
String p2Team = p2.getString("team");
int p2Goals = p2.getInt("goals");
int p2Reds = p2.getInt("reds");
int matchNum = match.getInt("match");
allPlayers[0] = new Player("p1", p1Team, p1Goals, p1Reds);
allPlayers[1] = new Player("p2", p2Team, p2Goals, p2Reds);
matches[i] = new Match(matchNum, allPlayers[0], allPlayers[1]);
}
}