2016-11-25 86 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對象字段名稱」返回大量結果。