除了凯文的回答,您可以遍历的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。 –