2012-08-27 37 views
0

任何人都可以請幫我把這個JSON轉換成Java對象。我通常使用GSON,但似乎這次它不適合我。問題是,我想創建一個包含Json to java對象android?

Class JsonGotText{ 
String status; 
List<Object> result; 
} 

但在列表中的所有對象都是差屬性的對象......所以我不知道該怎麼做才能讓GSON地圖它正確

{ 
    "status": 0, 
    "result": { 
    "1346053628": { 
     "type": "default", 
     "decorated_time": 1346053628, 
     "guidOwner": 13716, 
     "friendGuid": 3264, 
     "action_type": "friend", 
     "summary": " is now a friend with ", 
     "annotation": null, 
     "group": "" 
    }, 
    "1346051675": { 
     "type": "event", 
     "decorated_time": 1346051675, 
     "guidOwner": 90, 
     "title": null, 
     "action_type": "event_relationship", 
     "summary": "river:event_relationship:object:event", 
     "annotation": null, 
     "group": "" 
    }, 
    "1346048488": { 
     "type": "event", 
     "decorated_time": 1346048488, 
     "guidOwner": 90, 
     "title": null, 
     "action_type": "event_relationship", 
     "summary": "river:event_relationship:object:event", 
     "annotation": null, 
     "group": "" 
    } 
    } 
} 
+1

在你的JSON的「結果」的對象應該是一個數組。 – Sayyam

+0

是的,我認爲是這樣,但問題是在存儲差異對象的數組... ...你有什麼想法嗎? – vhong

回答

1

嘗試使用

Class JsonGotText{ 
String status; 
HashMap<String, Object> result; 
} 

如果性能不是關鍵標準在這裏。您最好使用'JSONObject'而不用擔心JSON字符串的結構。


理想情況下,您應該編寫一個POJO。說EventPOJO具有相同的每個結果對象持有的屬性,然後讓Java類

Class JsonGotText{ 
    String status; 
    HashMap<String, EventPOJO> result; 
} 

您可能需要使用類型令牌see here,但稍後會節省你的努力。

更新
看起來上面的句子聽起來很混亂。這裏是澄清什麼,我想EventPOJO代表。該EventPOJO將代表像

{ 
     "type": "event", 
     "decorated_time": 1346048488, 
     "guidOwner": 90, 
     "title": null, 
     "action_type": "event_relationship", 
     "summary": "river:event_relationship:object:event", 
     "annotation": null, 
     "group": "" 
    } 

UPDATE1 @LalitPoptani要求精確的工作代碼的東西。這裏是!

下面是一個工作示例:

public class Test { 

    private static String json = 
      "{"+ 
         "\"status\": 0,"+ 
         "\"result\": {"+ 
         "\"1346053628\": {"+ 
          "\"type\": \"default\","+ 
          "\"decorated_time\": 1346053628,"+ 
          "\"guidOwner\": 13716"+ 
         "},"+ 
         "\"1346051675\": {"+ 
          "\"type\": \"event\","+ 
          "\"decorated_time\": 1346051675,"+ 
          "\"guidOwner\": 90"+ 
         "},"+ 
         "\"1346048488\": {"+ 
          "\"type\": \"event\","+ 
          "\"decorated_time\": 1346048488,"+ 
          "\"guidOwner\": 90"+ 
         "}"+ 
         "}" + 
      "}"; 

    public static class Event{ 
     String type; 
     Long decorated_time; 
     Integer guidOwner; 
    } 

    public static class JSON{ 
     Integer status; 
     HashMap<Long, Event> result; 
    } 

    public static void main(String[] args){ 
     Gson gson = new Gson(); 
     System.out.println("JSON: " + json); 
     JSON j = gson.fromJson(json, JSON.class); 
     for(Entry<Long, Event> e: j.result.entrySet()){ 
      System.out.println(e.getKey() + ": " + e.getValue().guidOwner); 
     } 
    } 
} 
+0

@LalitPoptani好了,結果將進入哈希映射:)鍵將是地圖的關鍵字,POJO表示鍵映射到的鍵。我並不是說這些整數是屬性。你又是JsonGotObject嗎? – Nishant

+0

2件事。 1.如果他使用JSONObject,他可以做getJSONObject(「result」);然後獲取Keys並迭代到鍵中並獲取表示他的事件詳細信息的JSONObject。 2.如果他使用Gson,那麼他只會得到JsonGotText對象並在hashmp中迭代。簡單? – Nishant

+0

@LalitPoptani 1.我討厭喂勺子的人,他們應該學習。我沒有得到報酬。所以,取決於我,而不是你,詳細儘可能多的**我**。 3. OP詢問不懷疑你。你可以回答!他們可以更好地僱用我在谷歌。 :) – Nishant