如何在android中使用GSON创建JSONArray

时间:2017-02-24 11:05:23

标签: java android json

需要使用GSON类

创建JSONArray类型
{

"Company":"XYZ Company",
"Cust_Name":"ABC NAME",
"Sector":"SECTOR 1",
"Year":"2017",
"Product_IDs":[
    {
        "Product_ID":"001",
        "JAN":"20",
        "Feb":"30",
        "MAR":"40"
    },
    {
        "Product_ID":"002",
        "JAN":"50",
        "Feb":"60",
        "MAR":"80"
    }
]

}

如何创建JSONArray类型可以任何人帮助我

1 个答案:

答案 0 :(得分:0)

这样做

ProductM是您的模特

     Type type = new TypeToken<List<ProductM>>() {}.getType();
     Gson gson               = new Gson();
     List<ProductM> tempArr  = (List<ProductM>) gson.fromJson(productArray.toString(), type);// productArray -> your product array string


public class ProductM {

@SerializedName("Product_ID")
@Expose
private String productId;

@SerializedName("JAN")
@Expose
private String JAN;

@SerializedName("Feb")
@Expose
private String Feb;

@SerializedName("MAR")
@Expose
private String MAR;

public String getProductId() {
    return productId;
}

public String getJAN() {
    return JAN;
}

public String getFeb() {
    return Feb;
}

public String getMAR() {
    return MAR;
}
}