使用Gson </pojo>将内部ArrayList <pojo>转换为JSON

时间:2014-02-06 16:08:08

标签: java android json arraylist gson

我有ArrayList<TBSet>我要转换为JSON。 这个类有简单的字段和两个ArrayLists。当我尝试进行转换时,我得到空白ArrayLists,但它们不是空白的。

我使用此代码:

Type listOfTestObject = new TypeToken<ArrayList<TBSet>>(){}.getType();
json = new Gson().toJson(sets,listOfTestObject);

结果示例:

    [
   {
      "access":"public",
      "changed":"2014-02-04 20:10:04",
      "userId":"54086",
      "created":"2014-02-04 20:08:14",
      "pictureId":"ZWM3ODA5Njme7m7KbNyKlQ\u003d\u003d:::1391530205",
      "forWinter":0,
      "height":0,
      "forSummer":0,
      "isAutoNamed":0,
      "isLiked":0,
      "likeRating":0,
      "forSpring":0,
      "forAutumn":0,
      "deleted":0,
      "commentsCount":0,
      "width":0,
      "zoom":0
   }
]

这是TBSet对象:

public class TBSet {
    private String id;
    private String userId;
    private String guid;
    private String name;
    private String description;
    private String pictureId;
    private String access;
    private String created;
    private String changed;
    private int isAutoNamed;
    private int zoom;
    private int height;
    private int width;
    private int likeRating;
    private int commentsCount;
    private int forWinter;
    private int forAutumn;
    private int forSummer;
    private int forSpring;
    private int isLiked;
    private ArrayList<Integer> appointments;
    private ArrayList<TBSetObject> objects;
    private int deleted;

TBSetObject

public class TBSetObject {
    private String id;
    private String type;
    private String color;
    private String text;
    private String fontId;
    private int fontSize;
    private String fontStyle;
    private String pictureId;
    private int top;
    private int left;
    private int width;
    private int height;
    private int flip;
    private int flop;
    private int opacity;
    private int zIndex;
    private int rotate;
    private int isWithoutBackground;
    private int backgroundRemoveTolerance;
    private String backgroundRemovePath;
    private int isArticle;
    private int isSuperArticle;
    private int locked;
    private int background;
    private String articleId;

1 个答案:

答案 0 :(得分:1)

我相信您需要为每个属性添加@serializedvalue,并且只需使用:

Gson gson = new Gson();
String json = gson.toJson(arraylistObj);