使用Gson将POJO序列化为Json

时间:2016-05-12 02:38:27

标签: gson

Expected Output : 
[
  {
    "title" : "Dessert",
    "food_details": [
      {
        "name" : "leche plan",
        "price":"362", 
        "add_ons" : [
          {
            "extra": "add milk",
            "extra_price": 500
          }
        ]
      }
    ]
  }
]

嗨我需要像这样使用Gson作为序列化器的json输出

这是我的Model DealInfo.java

public class DealInfo {
private String title;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

private ArrayList<food_details> _food_details;

public ArrayList<food_details> getFood_details() {
    return _food_details;
}

public void setFood_details(ArrayList<food_details> food_details) {
    this._food_details = food_details;
}


public static class food_details{
    private String name;
    private String image;
    private String price;

    public food_details(String name, String price){
        this.name = name;
        this.price = price;
    }
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public static class add_ons{
    private String extra;
    private String extra_price;

    public add_ons(String extra, String extra_price){
        this.extra = extra;
        this.extra_price = extra_price;
    }
    public String getExtra_price() {
        return extra_price;
    }

    public void setExtra_price(String extra_price) {
        this.extra_price = extra_price;
    }

    public String getExtra() {

        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }
} 
}

这就是我创建json的方式

    DealInfo dealInfo = new DealInfo();
    ArrayList<DealInfo.food_details> detailsArrayList = new ArrayList<>();
    ArrayList<DealInfo.add_ons> addOnsArrayList = new ArrayList<>();
    detailsArrayList.add(new DealInfo.food_details("Foood 1", "666"));
    detailsArrayList.add(new DealInfo.food_details("Foood 2", "667"));
    detailsArrayList.add(new DealInfo.food_details("Foood 3", "668"));
    detailsArrayList.add(new DealInfo.food_details("Foood 4", "669"));
    detailsArrayList.add(new DealInfo.food_details("Foood 5", "660"));
    addOnsArrayList.add(new DealInfo.add_ons("Leche Flan", "653"));
    addOnsArrayList.add(new DealInfo.add_ons("Leche Con Yelo", "641"));
    addOnsArrayList.add(new DealInfo.add_ons("Leche Con Kasoy", "777"));
    Gson gson = new Gson();
    dealInfo.setTitle("My Own Title");
    dealInfo.setFood_details(detailsArrayList);
    String output = gson.toJson(dealInfo);
    Log.d("jsonstringoutput", output);

,输出就是这个

{
  "_food_details": [
    {
      "name": "Foood 1",
      "price": "666"
    },
    {
      "name": "Foood 2",
      "price": "667"
    },
    {
      "name": "Foood 3",
      "price": "668"
    },
    {
      "name": "Foood 4",
      "price": "669"
    },
    {
      "name": "Foood 5",
      "price": "660"
    }
  ],
  "title": "My Own Title"
}

问题是如何在food_details中添加add_ons json数组,就像预期的输出一样

提前致谢!

如果生病了就像这样的硬核方式,我不希望它像这样:

JSONArray mainArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    JSONObject foodObject = new JSONObject();
    JSONArray foodArray = new JSONArray();
    JSONObject addOnsObject = new JSONObject();
    JSONArray addOnsArray = new JSONArray();
    try {
        foodObject.put("name", "food 1");
        foodObject.put("price", "600");
        foodArray.put(foodObject);
        addOnsObject.put("extra", "milk");
        addOnsObject.put("extra_price", "150");
        addOnsArray.put(addOnsObject);
        jsonObject.put("food_details", foodArray);
        jsonObject.put("title", "My Own Food");
        foodObject.put("add_ons", addOnsArray);
        mainArray.put(jsonObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.d("jsonoutput", mainArray.toString());

1 个答案:

答案 0 :(得分:2)

import java.util.List;

public class DealInfo {
    public String title;
    public List<Food> foods;
}

...

public class Addon {
    private String extra;
    private String extra_price;

    public Addon(String extra, String extra_price) {
        this.extra = extra;
        this.extra_price = extra_price;
    }
}

...

import java.util.List;

public class Food {
    private String name;
    private String image;
    private String price;

    public List<Addon> addons;

    public Food(String name, String price){
        this.name = name;
        this.price = price;
    }
}

...

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.List;

public class JsonExample {

    public static void main(String[] args) {

        List<DealInfo> dealInfos = new ArrayList<DealInfo>();
        DealInfo dealInfo = new DealInfo();
        Food food = new Food("Food 1", "1000");
        List<Food> foods = new ArrayList<Food>();

        Addon addon = new Addon("extra 1", "100");
        List<Addon> addons = new ArrayList<Addon>();
        addons.add(addon);
        food.addons = addons;

        foods.add(food);
        dealInfo.foods = foods;
        dealInfo.title = "http://lenar.io :)";

        dealInfos.add(dealInfo);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(dealInfos);

        System.out.println(json);

    }

}

输出

[
  {
    "title": "http://lenar.io :)",
    "foods": [
      {
        "name": "Food 1",
        "price": "1000",
        "addons": [
          {
            "extra": "extra 1",
            "extra_price": "100"
          }
        ]
      }
    ]
  }
]

删除所有不重要的细节和方法