除了凱文的回答,您可以遍歷的JSONObject的keys():
JSONObject associative = loadJSONObject("associative.json");
JSONObject associativeData = associative.getJSONObject("data");
ArrayList<JSONArray> listA = new ArrayList<JSONArray>();
for(Object key : associativeData.keys()){
String keyName = (String)key;
JSONArray data = associativeData.getJSONArray(keyName);
println(keyName,"=",data);
listA.add(data);
}
System.err.println(listA);
associative.json:
{
"data":{
"abase":[
"put down",
"humiliate",
"cut down"
],
"abate":[
"diminish",
"let up"
],
"abbot":[
"monastery head",
"monastic title"
]
}
}
您也重新組織你的JSON數據,因此它適合你的目標。 目前,您在JSON對象(關聯數組)中有單詞和同義詞。 您可以輕鬆將其轉換爲JSON數組並構造數據,以便訪問/解析。例如: array.json
{
"data":[
{
"word":"abase",
"synonyms":[
"put down",
"humiliate",
"cut down"
]
},
{
"word":"abate",
"synonyms":[
"diminish",
"let up"
]
},
{
"word":"abbot",
"synonyms":[
"monastery head",
"monastic title"
]
}
]
}
您仍然可以使一個ArrayList,如果你想,但你不應該真的需要它,你可以輕鬆地訪問每一個字,直接同義詞。 它應該是簡單不必轉換/解析,只是訪問你所需要的:
ArrayList<JSONArray> listB = new ArrayList<JSONArray>();
JSONObject array = loadJSONObject("array.json");
JSONArray arrayData = array.getJSONArray("data");
for(int i = 0 ; i < arrayData.size(); i++){
JSONObject data = arrayData.getJSONObject(i);
println("\t",data.getString("word"),"=",data.getJSONArray("synonyms"));
listB.add(data.getJSONArray("synonyms"));
}
System.err.println(listB);
更新這裏的呈現在屏幕上的文本的示例:
import processing.data.*;
void setup(){
size(400,400);
background(0);
int textX = 10;
int textY = 20;
JSONObject array = loadJSONObject("array.json");
JSONArray arrayData = array.getJSONArray("data");
for(int i = 0 ; i < arrayData.size(); i++){
JSONObject data = arrayData.getJSONObject(i);
String word = data.getString("word");
JSONArray synonyms = data.getJSONArray("synonyms");
println(word,"=",synonyms);
//render on screen
text(word.toUpperCase(),textX,textY);
for(int j = 0 ; j < synonyms.size(); j++){
String synonym = synonyms.getString(j);
text(synonym,textX,textY + (textY * (j+1)));
}
//increment x position for next word
textX += 100;
}
}
更新2下面是一個封裝示例,它將鼠標懸停在單詞上時使用概念驗證提示顯示:
import processing.data.*;
ArrayList<Word> words = new ArrayList<Word>();
void setup(){
size(400,400);
int textX = 10;
int textY = 20;
JSONObject array = loadJSONObject("array.json");
JSONArray arrayData = array.getJSONArray("data");
for(int i = 0 ; i < arrayData.size(); i++){
JSONObject data = arrayData.getJSONObject(i);
String word = data.getString("word");
JSONArray synonyms = data.getJSONArray("synonyms");
println(word,"=",synonyms);
words.add(new Word(textX,textY,"hint #"+(i+1),data));
//increment x position for next word
textX += 100;
}
}
void draw(){
background(0);
for(Word word : words){
word.draw();
}
}
class Word{
String hint = "...";
JSONObject data;
float x,y;
float textWidth;
float textHeight = 20;
Word(float x,float y,String hint,JSONObject data){
this.x = x;
this.y = y;
this.hint = hint;
this.data = data;
textWidth = textWidth(data.getString("word"));
}
void draw(){
fill(255);
String word = data.getString("word");
JSONArray synonyms = data.getJSONArray("synonyms");
text(word.toUpperCase(),x,y);
for(int j = 0 ; j < synonyms.size(); j++){
String synonym = synonyms.getString(j);
text(synonym,x,y + (textHeight * (j+1)));
}
fill(0,192,0);
//hint tooltip
//if mouse within word bounding box
if((mouseX >= x && mouseX <= x + textWidth) &&
(mouseY >= y-textHeight && mouseY <= y)){
//render the text at mouse coordinates
//be aware that y is the base of the text -> be sure to check out the reference for text functions (e.g. textAscent(),textDescent(),etc.)
text(hint,mouseX,mouseY+textHeight);
}
}
}
爲什麼要手動解析JSON文件?難道你不能只用一個庫來解析JSON如** GSON **或** Jackson **嗎? – misko321
那麼,我正在學習數據結構並與文件進行交互,所以我現在正試圖很好地理解JSON。 –