Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中使用GSON创建JSONArray_Java_Android_Json - Fatal编程技术网

Java 如何在android中使用GSON创建JSONArray

Java 如何在android中使用GSON创建JSONArray,java,android,json,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

我需要使用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类型任何人都能帮我这样做吗

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;
}
}
Type Type=new-TypeToken(){}.getType();
Gson Gson=新的Gson();
List tempArr=(List)gson.fromJson(productArray.toString(),type);//productArray->您的产品数组字符串
公共类产品{
@SerializedName(“产品标识”)
@暴露
私有字符串productId;
@序列化名称(“一月”)
@暴露
私家弦琴;
@序列化名称(“二月”)
@暴露
私人字符串;
@序列化名称(“MAR”)
@暴露
私人字符串MAR;
公共字符串getProductId(){
返回productId;
}
公共字符串getJAN(){
返回一月;
}
公共字符串getFeb(){
2月返回;
}
公共字符串getMAR(){
返回MAR;
}
}
你喜欢这样吗

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;
}
}
Type Type=new-TypeToken(){}.getType();
Gson Gson=新的Gson();
List tempArr=(List)gson.fromJson(productArray.toString(),type);//productArray->您的产品数组字符串
公共类产品{
@SerializedName(“产品标识”)
@暴露
私有字符串productId;
@序列化名称(“一月”)
@暴露
私家弦琴;
@序列化名称(“二月”)
@暴露
私人字符串;
@序列化名称(“MAR”)
@暴露
私人字符串MAR;
公共字符串getProductId(){
返回productId;
}
公共字符串getJAN(){
返回一月;
}
公共字符串getFeb(){
2月返回;
}
公共字符串getMAR(){
返回MAR;
}
}