0

このJSONをJavaオブジェクトに変換するのを手伝ってくれませんか。普段はGSONを使っているのですが、今回はうまくいかないようです。問題は、含むオブジェクトを作成したいということです

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": ""
    }
  }
}
4

1 に答える 1

1

Try using

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

If performance is not key criteria here. You better use 'JSONObject' without worrying the structure of JSON String.


Ideally, you should write a POJO. Say EventPOJO that has attributes same as as each result object holds and then make the Java class as

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

you may have to use a type token see here, but will save your efforts later.

Update
It seems the sentence above sounds confusing. Here is clarification what I wanted EventPOJO to represent to. The EventPOJO will represents things like

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

Update1 @LalitPoptani asked for exact working code. Here it is!

Here is an working example:

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 に答える