0

谁能帮我将此 JSON 转换为 Java 对象。我通常使用 GSON,但这次似乎对我不起作用。问题是我想创建一个包含

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

但是List中的所有对象都是不同的属性......所以我不知道如何让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": ""
    }
  }
}
4

1 回答 1

1

尝试使用

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

如果性能不是这里的关键标准。您最好使用“JSONObject”而不用担心 JSON 字符串的结构。


理想情况下,您应该编写一个 POJO。假设EventPOJO具有与每个结果对象相同的属性,然后将 Java 类设为

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

您可能必须使用类型标记,请参见此处,但稍后会节省您的努力。

更新
似乎上面的句子听起来令人困惑。这是我想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);
        }
    }
}
于 2012-08-27T10:11:59.237 回答