ケビンの答えに加えて、あなたは、反復処理することができます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:それはあなたの目標に合うように、あなたはまた、あなたのJSONデータを再編成することができ
{
"data":{
"abase":[
"put down",
"humiliate",
"cut down"
],
"abate":[
"diminish",
"let up"
],
"abbot":[
"monastery head",
"monastic title"
]
}
}
。 現在、JSONオブジェクト(結合配列)に単語と同義語があります。 これをJSON配列に簡単に変換し、アクセス/解析が容易なようにデータを構造化することができます。例:あなたがしたい場合は、まだのArrayListを作ることができますが、あなたが本当にそれを必要としないはず
{
"data":[
{
"word":"abase",
"synonyms":[
"put down",
"humiliate",
"cut down"
]
},
{
"word":"abate",
"synonyms":[
"diminish",
"let up"
]
},
{
"word":"abbot",
"synonyms":[
"monastery head",
"monastic title"
]
}
]
}
array.json、あなたは簡単に、直接各単語と同義語にアクセスすることができます。 それは/変換解析し、ちょうどあなたが必要なものにアクセスする必要がない単純でなければなりません:
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ファイルを手動で解析したいですか? ** GSON **や** Jackson **のようなJSONの解析にはライブラリを使用できませんか? – misko321
さて、私はデータ構造を学び、ファイルとやりとりしているので、私はこの時点でJSONをよく理解しようとしています。 –