2016-11-25 91 views
2

所以我有一个JSON文件与多个数组。 “数据”包含大量包含每个单词的“线索”的单词。我用一个数组解析了一个JSON文件,但我不确定如何解析它。我知道JSONObjects被{}包围,JSONArray被[]包围,所以“data”应该是JSONObject,每个单词都是一个数组。如何解析具有多个阵列的JSON文件?

{ 
    "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"); 
} 

我可以创建JSONArrays的ArrayList和该文件以在添加每个阵列ArrayList与for-each?对不起,如果我对我的问题没有100%清楚,很难对某个你不明白的问题提出一个问题,但如果你需要澄清,请告诉我,我很乐意解释这个问题。

编辑︰我正在使用处理/ Java

+1

为什么要手动解析JSON文件?难道你不能只用一个库来解析JSON如** GSON **或** Jackson **吗? – misko321

+0

那么,我正在学习数据结构并与文件进行交互,所以我现在正试图很好地理解JSON。 –

回答

2

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

} 

json text parse and preview

更新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); 
    } 


    } 


} 
+0

感谢您的回应乔治。我在Kevin的例子中看到,我需要输入该JSON文件中的每个单词,因为它是自己独立的JSONArray对象。在你的例子中,你循环遍历“associativeData”中的所有“键”,然后将该键的名称传递给一个String,然后将它传递给getJSONArray()?一旦我到达那里,就像我在帖子中使用的例子一样? –

+1

上面的第一个例子使用的JSON数据格式与您在问题/帖子中的格式相同。唯一的区别是我将你指向'keys()'对象,它允许你循环使用数据而不是手动使用每个单词。 你的问题没有解释的是你打算如何使用数据。也许有一种更简单的方式来构建和访问它? –

+0

我打算把所有的单词与他们的提示放到一个Word对象中,该对象包含单词本身,以及一个包含单词提示的ArrayList。构造函数会是这样的:'public Word(String name,ArrayList hints)。我应该能够得到所需的数据,对吗? –

1

将问题分解成更小的步骤。

第1步:你可以得到在JSON内的data对象吗?

您可以通过调用loadJSONObject()函数加载所有的JSON的,然后调用getJSONObject()才能到data对象做到这一点:

JSONObject的JSON = loadJSONObject( “data.json”); JSONObject data = json.getJSONObject(“data”); println(data);

第2步:你能到达data对象内的三个字段吗?

我不认为有一个标准的开箱即用的方式来循环使用处理的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获取json对象字段名称”返回大量结果。