2017-06-12 47 views
0

我遇到麻煩GSON:的Java GSON檢查數據

比如我有一個從網站輸出:

[["connected"], ["user1":"Hello"], ["user2":"Hey"], ["disconnected"]]

但我想解析此JSON和輸出是這樣的:

connected 
user1 says: Hello 
user2 says: Hey 
disconnected 

我quicly寫了這個代碼:

public static void PrintEvents(String id){ 
    String response = Post.getResponse(Server()+"events?id="+id,""); 
    // response is [["connected"],["user1":"Hello"],["user2":"Hey"],["disconnected"]] 

    JsonElement parse = (new JsonParser()).parse(response); //found this in internet 

    int bound = ????????????; // Should be 4 

    for (int i=1;i<=bound;i++){ 
     String data = ???????????; 
     if (data == "connected" || data == "disconnected") then { 
      System.out.println(data); 
     }else if(?????==2){// to check how many strings there is, if it's ["abc","def"] or ["abc"] 
      String data2 = ??????????????; 
      System.out.println(data+" says: "+data2); 
     }else{ 
      //something else 
     } 
    }; 

} 

我應該使用問號插入到這些部件以使代碼有效嗎?

我找不到任何方式,使其工作...

對不起,我的英語不好。

編輯:改變了對[["connected"], ["user1","Hello"], ["user2","Hey"], ["disconnected"]]的迴應。早期的迴應是無效的JSON。

回答

0
  1. 您已經粘貼的響應不是有效的json。將其粘貼到http://www.jsoneditoronline.org/並查看錯誤。

請找到下面的代碼片斷:

公共靜態無效printEvents(字符串ID) { 字符串響應= 「[[\」 連接\ 「],[\」 USER1:你好\「] ,[\ 「用戶2:喂\」],[\ 「斷開\」]]「;

JsonElement parse = (new JsonParser()).parse(response); //found this in internet 


    int bound = ((JsonArray)parse).size(); // Should be 4 

    for (int i = 0; i < bound; i++) { 
     String data = ((JsonArray)parse).get(0).getAsString(); 
     if (data.equals("connected") || data.equals("disconnected")) { 
      System.out.println(data); 
      continue; 
     } 
     String[] splittedData = data.split(":"); 
     if (splittedData.length 
       == 2) {// to check how many strings there is, if it's ["abc","def"] or ["abc"] 
      System.out.println(splittedData[0] + " says: " + splittedData[1]); 
     } 
     /* 
     *else{ 
     * your else logic goes here 
     * } 
     * */ 
    } 

} 

夫婦的建議:

  1. 如果你是新的JSON的世界,用傑克遜,而不是GSON。
  2. 響應不是一個好設計。略正確JSON:

{ 「firstKey」: 「連接」, 「userResponses」:[ { 「用戶1」: 「哎」 }, { 「用戶2」: 「喜」 } ], 「的lastKey」: 「斷開」 }

  • 另外嘗試定義的,而不是使用JSON直列工作的POJO。
  • +0

    您的迴應'[[「連接了」[[「connected」],[「user1」:「Hello」],[「user2」:「Hey」],[「 「],[」user1:Hello「],[」user2:Hey「],[」disconnected「]] 您的代碼有效,但是當我更改正確的響應時,它不起作用。 'com.google.gson.stream.MalformedJsonException:在第1行的未終止數組第26列路徑$ [1] [1]' –

    +0

    您所說的響應甚至不是有效的json。在數組中的json中,不能像<「user1」:「Hello」>那樣輸入內容。如果你想在同一個結構中,你應該改變你的響應格式。上面給出了一種可能的格式。您可以查找的另一種格式: [[「connected」],[{「user1」:「Hello」}],[{「user2」:「Hey」}],[「disconnected」]] –

    +0

    我懂了加工。非常感謝,我將它改爲'[[「connected」],[「user1」,「Hello」],[「user2」,「Hey」],[「disconnected」]]'使其成爲有效的json。 –

    0

    您需要定義一個單獨的類是這樣的:

    class MyClass{ 
    String name; 
    String value; 
    } 
    

    然後:

    List<MyClass> myclasses = new Gson().fromJson(response, new TypeToken<List<MyClass>>(){}.getType()); 
    

    然後

    for(MyClass myclass: myclasses){ 
    ... 
    }