2013-08-04 17 views
1

解析這是我的JSON數組:循環不打破 - JSON與GSON

[ 
     [ 36, 
     100, 
     "The 3n + 1 problem", 
     56717, 
     0, 
     1000000000, 
     0, 
     6316, 
     0, 
     0, 
     88834, 
     0, 
     45930, 
     0, 
     46527, 
     5209, 
     200860, 
     3597, 
     149256, 
     3000, 
     1 
     ], 
     [ 
     ........ 
     ], 
     [ 
     ........ 
     ], 
     .....// and almost 5000 arrays like above 
] 

我想需要EAH陣列的前四個值並跳過值的其餘部分,如:

36 100 "The 3n + 1 problem" 56717 

這是我寫到目前爲止代碼:

reader.beginArray(); 
while (reader.hasNext()) { 
    reader.beginArray(); 
    while (reader.hasNext()) { 
     System.out.println(reader.nextInt() + " " + reader.nextInt() 
          + " " + reader.nextString() + " " 
          + reader.nextInt()); 
     for (int i = 0; i < 17; i++) { 
      reader.skipValue(); 
     } 
     reader.skipValue(); 
    } 
    reader.endArray(); 
    System.out.println("loop is break"); // this is not printed as the inner loop is not breaking 
} 
reader.endArray(); 
reader.close(); 

而且如我所料正在打印:

36 100 "The 3n + 1 problem" 56717 
................................. 
.................................. 
1049 10108 The Mosquito Killer Mosquitos 49 
1050 10109 Solving Systems of Linear Equations 129 
1051 10110 Light, more light 9414 
1052 10111 Find the Winning Move 365 

這是行得通的,但內循環沒有正確地分解。我的代碼有什麼問題?我錯過了什麼,以便我的代碼無法正常工作?

編輯:(解決方案) 我結束了此解決方案:

reader.beginArray(); 
      while (reader.hasNext()) { 
       reader.beginArray(); 
       // read and parse first four elements, checking hasNext() each time for robustness 
       int a = reader.nextInt(); 
       int b = reader.nextInt(); 
       String c = reader.nextString(); 
       int d = reader.nextInt(); 
       System.out.println(a + " " + b + " " + c + " " + d); 
       while (reader.hasNext()) 
        reader.skipValue(); 
       reader.endArray(); 
      } 
      reader.endArray(); 
+0

請問您可以指定您正在使用Java讀取json的庫。 –

+0

這是'谷歌'谷歌 –

回答

2

你不想叫跳略值()或下一個*如果hasNext()返回false。 GSON documentation建議先累計值。關於這一主題的變化是:

reader.beginArray(); 
while (reader.hasNext()) { 
    int position; 
    int a, b, d; // stores your parsed values 
    String c; // stores your parsed values 
    reader.beginArray(); 
    // read and parse first four elements, checking hasNext() each time for robustness 
    for (position = 0; position < 4 && reader.hasNext(); ++ position) { 
     if (position == 0) a = reader.nextInt(); 
     else if (position == 1) b = reader.nextInt(); 
     else if (position == 2) c = reader.nextString(); 
     else if (position == 3) d = reader.nextInt(); 
    } 
    // if position < 4 then there weren't enough values in array. 
    if (position == 4) { // correctly read 
     System.out.println(a + " " + b + " " + c + " " + d); 
    } 
    // skip rest of array, regardless of number of values 
    while (reader.hasNext()) 
     reader.skipValue(); 
    reader.endArray(); 
} 
reader.endArray(); 

注意,還有很多其他的方法來分析這些前4個值,使用任何有意義您的具體情況(如將它們存儲在一個列表第一,或將它們存儲爲字符串然後再解析,或者任何你想要的 - 重點是,不要對數組中元素的數量做出假設,堅持規則並在讀取每個元素之前使用hasNext()。

+0

先生,這是完美的工作?我有一個很大的Json feed,我用android內置'JSONparser'解析json。爲了提高性能,我現在正在使用'GSON'。你認爲上述方法是最快的方法嗎?或者有什麼辦法可以進一步優化? –

+1

你可以做很多事情來優化它。上述代碼按照其文檔使用GSON,這是您可以做的最好的。耗時的部分是GSON的解析器代碼本身,您無法控制它。無論如何,你並沒有提出正確的問題。不要過早優化。首先讓你的代碼工作,然後*如果它不符合你的性能要求,找到你的瓶頸並開始考慮優化。 –

+0

謝謝你的一些偉大的概念提示也:) –