如何用gson解析这个结果数组?我坚持了两天我无法解析。 从SO和其他几个地方,我发现我需要定义一个顶级容器,但我不知道如何完成它的定义。
results:[
{
"SupplierCatalog": {
"supplier_catalog_id": "139",
"distributor_id": "57",
"distributor_asking_price": "999.99",
"supplier_id": null,
"product_name": "jjjjjjjj j j j j j j jj j jjjjjjjjjjjj",
"product_description": "kkkkkkkkkkkkkk k k k k"
},
"image_details": {
"isCustomImageProvided": 0,
"isImageUploadedTo": 1,
}
},
{
"SupplierCatalog": {
"supplier_catalog_id": "138",
"distributor_id": "57",
"distributor_asking_price": "69.25",
"supplier_id": null,
"product_name": "i+am+editing+this",
"product_description": "using+wait+for+it..........+the+API"
},
"image_details": {
"isCustomImageProvided": 1,
"isImageUploadedTo": 1,
}
},
{
"SupplierCatalog": {
"supplier_catalog_id": "137",
"distributor_id": "57",
"distributor_asking_price": "69.69",
"supplier_id": null,
"product_name": "Supplier Created Product Sample",
"product_description": "This is a sample description"
},
"image_details": {
"isCustomImageProvided": 1,
"isImageUploadedTo": 1,
}
},
{
"SupplierCatalog": {
"supplier_catalog_id": "136",
"distributor_id": "57",
"distributor_asking_price": "45.58",
"supplier_id": null,
"product_name": "Distributor Created Product Sample",
"product_description": "Blah Blah Blah"
},
"image_details": {
"isCustomImageProvided": 1,
"isImageUploadedTo": 1,
}
},
{
"SupplierCatalog": {
"supplier_catalog_id": "135",
"distributor_id": "57",
"distributor_asking_price": "99.99",
"supplier_id": null,
"product_name": "Distributor Created Product Sample",
"product_description": "In publishing and graphic design, lorem ipsum is a filler text commonly used to demonstrate the graphic elements of a document or visual presentation. Replacing meaningful content that could be distracting with placeholder text may allow viewers to focus on graphic aspects such as font, typography, and page layout.\r\n\r\nThe lorem ipsum text is typically a scrambled section of De finibus bonorum et malorum, a 1st-century BC Latin text by Cicero, with words altered, added, and removed such that it is nonsensical, improper Latin.\r\n\r\nA variation of the ordinary lorem ipsum text has been used in typesetting since the 1960s or earlier, when it was popularized by advertisements for Letraset transfer sheets. It was introduced to the Information Age in the mid-1980s by Aldus Corporation, which employed it in graphics and word processing templates for its desktop publishing program, PageMaker, for the Apple Macintosh.[1]"
},
"image_details": {
"isCustomImageProvided": 1,
"isImageUploadedTo": 1,
}
}
]
我无法解析制作两节课。所以请帮忙。 之后我怎么得到个人获取方法。感谢。
答案 0 :(得分:1)
以下是结构可能的示例,具体取决于您的json结果,它可能会略有变化。
package com.example.test;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json = "{results:[{\"SupplierCatalog\":{\"supplier_catalog_id\": \"139\",\"distributor_id\": \"57\",\"distributor_asking_price\": \"999.99\",\"supplier_id\": null,\"product_name\": \"jjjjjjjj j j j j j j jj j jjjjjjjjjjjj\",\"product_description\": \"kkkkkkkkkkkkkk k k k k\"},\"image_details\": {\"isCustomImageProvided\": 0,\"isImageUploadedTo\": 1}}]}";
JsonParseResult jsonParseResult = new Gson().fromJson(json, JsonParseResult.class);
if (jsonParseResult != null && jsonParseResult.getResult() != null) {
for (Result result : jsonParseResult.getResult()) {
Log.d(TAG, "Result: " + result.toString());
}
}
}
public class JsonParseResult {
@SerializedName("results")
private List<Result> results;
public JsonParseResult(List<Result> results) {
super();
this.results = results;
}
public List<Result> getResult() {
return results;
}
public void setResult(List<Result> results) {
this.results = results;
}
}
public class Result {
@SerializedName("SupplierCatalog")
private SupplierCatalog supplierCatalog;
@SerializedName("image_details")
private ImageDetails imageDetails;
public Result(SupplierCatalog supplierCatalog, ImageDetails imageDetails) {
super();
this.supplierCatalog = supplierCatalog;
this.imageDetails = imageDetails;
}
public SupplierCatalog getSupplierCatalog() {
return supplierCatalog;
}
public void setSupplierCatalog(SupplierCatalog supplierCatalog) {
this.supplierCatalog = supplierCatalog;
}
public ImageDetails getImageDetails() {
return imageDetails;
}
public void setImageDetails(ImageDetails imageDetails) {
this.imageDetails = imageDetails;
}
@Override
public String toString() {
return "Result [supplierCatalog=" + supplierCatalog + ", imageDetails=" + imageDetails + "]";
}
}
public class SupplierCatalog {
@SerializedName("supplier_catalog_id")
private Integer supplierCatalogId;
@SerializedName("distributor_id")
private Integer distributorId;
@SerializedName("distributor_asking_price")
private Double distributorAskingPrice;
@SerializedName("supplier_id")
private Integer supplierId;
@SerializedName("product_name")
private String productName;
@SerializedName("product_description")
private String productDescription;
public SupplierCatalog(Integer supplierCatalogId, Integer distributorId, Double distributorAskingPrice, Integer supplierId, String productName, String productDescription) {
super();
this.supplierCatalogId = supplierCatalogId;
this.distributorId = distributorId;
this.distributorAskingPrice = distributorAskingPrice;
this.supplierId = supplierId;
this.productName = productName;
this.productDescription = productDescription;
}
public Integer getSupplierCatalogId() {
return supplierCatalogId;
}
public void setSupplierCatalogId(Integer supplierCatalogId) {
this.supplierCatalogId = supplierCatalogId;
}
public Integer getDistributorId() {
return distributorId;
}
public void setDistributorId(Integer distributorId) {
this.distributorId = distributorId;
}
public Double getDistributorAskingPrice() {
return distributorAskingPrice;
}
public void setDistributorAskingPrice(Double distributorAskingPrice) {
this.distributorAskingPrice = distributorAskingPrice;
}
public Integer getSupplierId() {
return supplierId;
}
public void setSupplierId(Integer supplierId) {
this.supplierId = supplierId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
@Override
public String toString() {
return "SupplierCatalog [supplierCatalogId=" + supplierCatalogId + ", distributorId=" + distributorId + ", distributorAskingPrice=" + distributorAskingPrice + ", supplierId=" + supplierId + ", productName=" + productName + ", productDescription=" + productDescription + "]";
}
}
public class ImageDetails {
@SerializedName("isCustomImageProvided")
private Integer isCustomImageProvided;
@SerializedName("isImageUploadedTo")
private Integer isImageUploadedTo;
public ImageDetails(Integer isCustomImageProvided, Integer isImageUploadedTo) {
super();
this.isCustomImageProvided = isCustomImageProvided;
this.isImageUploadedTo = isImageUploadedTo;
}
public Integer getIsCustomImageProvided() {
return isCustomImageProvided;
}
public void setIsCustomImageProvided(Integer isCustomImageProvided) {
this.isCustomImageProvided = isCustomImageProvided;
}
public Integer getIsImageUploadedTo() {
return isImageUploadedTo;
}
public void setIsImageUploadedTo(Integer isImageUploadedTo) {
this.isImageUploadedTo = isImageUploadedTo;
}
@Override
public String toString() {
return "ImageDetails [isCustomImageProvided=" + isCustomImageProvided + ", isImageUploadedTo=" + isImageUploadedTo + "]";
}
}
}