[英]How do I parse a JSON file with multiple arrays?
So I have a JSON File with multiple arrays. 所以我有一个带有多个数组的JSON文件。 "data" contains a whole bunch of words that contain "clues" to each word. “数据”包含一大堆单词,每个单词都包含“线索”。 I've parsed up a JSON file with one array but I'm unsure of how to parse this up. 我已经解析了一个数组的JSON文件,但不确定如何解析它。 I know that JSONObjects are surrounded by {} and JSONArrays are surrounded by [], so "data" should be a JSONObject and each word an array. 我知道JSONObjects被{}包围,而JSONArrays被[]包围,因此“数据”应该是一个JSONObject,每个单词都是一个数组。
{
"data":{
"abase":[
"put down",
"humiliate",
"cut down",
],
"abate":[
"diminish",
"let up",
],
"abbot":[
"monastery head",
"monastic title",
]
}
}
I can parse up a JSON file with a singular array and create objects from the data as such: 我可以解析具有单个数组的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");
}
Can I create an ArrayList of JSONArrays and add each array in the file to that ArrayList with a for-each? 我可以创建一个JSONArrays的ArrayList并使用for-each将文件中的每个数组添加到该ArrayList吗? Sorry if I'm not 100% clear on my question, it's hard to phrase a question about something you don't understand, but if you need clarification, please let me know, I'd be glad to explain in further detail. 抱歉,如果我不能百分百清楚我的问题,很难说出您不理解的问题,但是如果您需要澄清,请告诉我,我很乐意进一步详细解释。
Edit: I'm using Processing/Java 编辑:我正在使用处理/ Java
In addition to Kevin's answer, you can iterate over the JSONObject's keys() : 除了Kevin的答案,您还可以遍历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: associative.json:
{
"data":{
"abase":[
"put down",
"humiliate",
"cut down"
],
"abate":[
"diminish",
"let up"
],
"abbot":[
"monastery head",
"monastic title"
]
}
}
You could also re-organize your JSON data so it fits your goal. 您还可以重新组织JSON数据,使其符合您的目标。 Currently you have words and synonyms in a JSON object (associative array). 当前,您在JSON对象(关联数组)中具有单词和同义词。 You could easily convert that to a JSON array and structure the data so it's easy to access/parse. 您可以轻松地将其转换为JSON数组并结构化数据,以便轻松访问/解析。 For example: array.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"
]
}
]
}
You can still make an ArrayList if you want to, but you shouldn't really need it, you can easily access each word and synonyms directly. 如果愿意,您仍然可以创建一个ArrayList,但实际上并不需要它,可以轻松地直接访问每个单词和同义词。 It should be simpler not having to convert/parse and just access what you need: 不必转换/解析并仅访问所需的内容,应该会更简单:
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);
Update here's an example that renders the text on screen: 更新这里是一个在屏幕上呈现文本的示例:
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;
}
}
Update 2 Here's an encapsulation example that uses a proof of concept hint display when hovering over a word: 更新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);
}
}
}
Break your problem down into smaller steps. 将您的问题分解为更小的步骤。
Step 1: Can you get to the data
object inside the JSON? 步骤1:您能否进入JSON中的data
对象?
You can do this by calling the loadJSONObject()
function to load all of the JSON, then calling getJSONObject()
to get to the data
object: 您可以通过调用loadJSONObject()
函数来加载所有JSON,然后调用getJSONObject()
来获取data
对象来做到这一点:
JSONObject json = loadJSONObject("data.json"); JSONObject json = loadJSONObject(“ data.json”); JSONObject data = json.getJSONObject("data"); JSONObject data = json.getJSONObject(“ data”); println(data); 的println(数据);
Step 2: Can you get to the three fields inside the data
object? 步骤2:您能否进入data
对象内的三个字段?
I don't think there's a standard, out-of-the-box way to loop over each of the fields using Processing's JSONObject
. 我认为没有一种标准的,即用的方法可以使用Processing的JSONObject
遍历每个字段。 So you'll have to get them manually: 因此,您必须手动获取它们:
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);
Step 3: Now that you have the JSONArray
instances, what do you want to do with them? 步骤3:现在您有了JSONArray
实例,您要如何处理它们?
Once you have the JSONArray
instances, you can do whatever you want with them, including looping over each of them and adding their contents to a single ArrayList
. 拥有JSONArray
实例后,就可以对它们执行任何操作,包括遍历每个实例并将其内容添加到单个ArrayList
。
If you want to write code that loops over the fields themselves, then you'll have to write that yourself. 如果要编写循环遍历字段本身的代码,则必须自己编写。 Googling "Java get json object field names" returns a ton of results. 谷歌搜索“ Java get json对象字段名称”将返回大量结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.