Java 如何解析具有多个数组的JSON文件?
所以我有一个包含多个数组的JSON文件。“数据”包含一大堆单词,其中包含每个单词的“线索”。我已经用一个数组解析了一个JSON文件,但我不确定如何解析它。我知道JSONObject被{}包围,JSONArray被[]包围,所以“数据”应该是JSONObject,每个单词都是数组Java 如何解析具有多个数组的JSON文件?,java,arrays,json,processing,Java,Arrays,Json,Processing,所以我有一个包含多个数组的JSON文件。“数据”包含一大堆单词,其中包含每个单词的“线索”。我已经用一个数组解析了一个JSON文件,但我不确定如何解析它。我知道JSONObject被{}包围,JSONArray被[]包围,所以“数据”应该是JSONObject,每个单词都是数组 { "data":{ "abase":[ "put down", "humiliate", "cut down", ], "abate":[
{
"data":{
"abase":[
"put down",
"humiliate",
"cut down",
],
"abate":[
"diminish",
"let up",
],
"abbot":[
"monastery head",
"monastic title",
]
}
}
我可以用单个数组解析JSON文件,并从数据中创建如下对象:
JSONObject allThings = loadJSONObject("filename.json");
JSONArray arr = getJSONArray("titleofarray");
for (int x = 0; x<=arr.size(); x++){
String fieldOne = arr.getJSONObject(x).getString("firstField");
int fieldTwo = arr.getJSONObject(x).getInt("secondField");
}
JSONObject allThings=loadJSONObject(“filename.json”);
JSONArray arr=getJSONArray(“标题阵列”);
对于(intx=0;x将问题分解为更小的步骤
第1步:您可以访问JSON中的数据对象吗
您可以通过调用loadJSONObject()
函数来加载所有JSON,然后调用getJSONObject()
来获取数据
对象:
JSONObject json=loadJSONObject(“data.json”);
JSONObject data=json.getJSONObject(“数据”);
println(数据)
第2步:你能找到数据对象中的三个字段吗
我认为没有一种标准的、现成的方法可以使用Processing的JSONObject
在每个字段上循环。因此,您必须手动获取它们:
JSONObject json = loadJSONObject("data.json");
JSONObject data = json.getJSONObject("data");
JSONArray abase = data.getJSONArray("abase");
JSONArray abate = data.getJSONArray("abate");
JSONArray abbot = data.getJSONArray("abbot");
println(abase);
println(abate);
println(abbot);
步骤3:现在您有了JSONArray
实例,您想对它们做什么
一旦拥有了JSONArray
实例,就可以对它们执行任何操作,包括在每个实例上循环并将其内容添加到单个ArrayList
如果您想编写在字段本身上循环的代码,那么您必须自己编写。谷歌搜索“Java get json object field names”会返回大量结果。除了Kevin的答案之外,您还可以迭代JSONObject的:
您还可以重新组织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);
ArrayList listB=新的ArrayList();
JSONObject数组=loadJSONObject(“array.json”);
JSONArray arrayData=array.getJSONArray(“数据”);
对于(int i=0;i
更新下面是一个在屏幕上呈现文本的示例:
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;
}
}
导入处理。数据。*;
无效设置(){
尺寸(400400);
背景(0);
int textX=10;
int textY=20;
JSONObject数组=loadJSONObject(“array.json”);
JSONArray arrayData=array.getJSONArray(“数据”);
对于(int i=0;i
更新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);
}
}
}
导入处理。数据。*;
ArrayList words=新的ArrayList();
无效设置(){
尺寸(400400);
int textX=10;
int textY=20;
JSONObject数组=loadJSONObject(“array.json”);
JSONArray arrayData=array.getJSONArray(“数据”);
对于(int i=0;i=x&&mouseX=y-textHeight&&mouseY),请务必查看文本函数的引用(例如textAscent()、textDescent()等)
文本(提示、鼠标、鼠标+文本高度);
}
}
}
为什么要手动解析JSON文件?难道你不能使用库解析GSON或Jackson之类的JSON吗?我正在学习数据结构和与文件交互,所以我试图在这一点上很好地理解JSON。感谢你的回答George。我在Kevin的例子中看到,我需要输入每一个单词另一方面,在您的示例中,您循环“AssociateData”中的所有“键”,然后将键的名称传递到字符串中,然后将其传递到getJSONArray()中?一旦到达,它就像我在文章中使用的示例一样?上面的第一个示例使用JS
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;
}
}
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);
}
}
}