我从json文件中获取数据,并希望在android中使用pojo类读取数据。我尝试了一些来自互联网的解决方案但我找不到确切的解决方案请帮帮我。
{
"data":[
{
"id":"7",
"parent_id":"3",
"catalogue_code":"vol 01",
"catalogue_name":"Devika",
"image":"images\/catalogue_image\/saree\/201Dev.jpg",
"image_code":"201",
"price":"1905",
"description":"\"Saree Fabrics : Georgette ,\r\nBlouse Fabrics : Raw Silk ,\r\n Blouse Length : 0.80 Mtr\r\nSaree with Blouse : 6.4 Mtr\r\nWidth of Saree : 43 Inches\r\nWork : Designer Embroidered Work,\r\nLook your traditional best with this utmost graceful and sophisticated Kvsfab Orange and Red Georgette Embroidered Saree.Available with Red Raw Silk customizable blouse fabric. Blouse shown in the image is for photography purpose . All accessories shown in the image are for photography purpose.(Slight variation in color is possible)\"\r\n",
"color":"Orange and Red",
"fabric":"Georgette",
"innner":"",
"fabric_top":"",
"fabric_bottom":"",
"fabric_dupatta":"",
"dupatta":"",
"size":"",
"length":"6.5 mtr with blouse",
"work":"Embroidered",
"style":"Half N Half",
"occasion":"Party, wedding",
"stitch_unstitch":"",
"blouse":"Half N Half",
"date":"2015-07-29 12:18:26",
"main_category":"Sarees",
"sub_category":"Designer Sarees"
},
{
"id":"8",
"parent_id":"3",
"catalogue_code":"vol 01",
"catalogue_name":"Devika",
"image":"images\/catalogue_image\/saree\/202Dev.jpg",
"image_code":"202",
"price":"1905",
"description":"Drape yourself in a distinct aura with this Kvsfab Green and Blue Georgette Embroidered Saree.Available with Blue Raw Silk customizable blouse fabric. Blouse shown in the image is for photography purpose . All accessories shown in the image are for photography purpose.(Slight variation in color is possible)\r\n",
"color":"Green and Blue",
"fabric":"Georgette",
"innner":"",
"fabric_top":"",
"fabric_bottom":"",
"fabric_dupatta":"",
"dupatta":"",
"size":"",
"length":"6.5 mtr with blouse",
"work":"Embroidered",
"style":"Half N Half Saree",
"occasion":"Party",
"stitch_unstitch":"",
"blouse":"Half N Half",
"date":"2015-07-29 12:18:26",
"main_category":"Sarees",
"sub_category":"Designer Sarees"
}
]
}
到目前为止我的代码是什么。
jsonobject包含上面的json数据
Gson gson = new GsonBuilder().create();
Log.d("JSON",jsonObject.toString());
String jsonOutput = jsonObject.toString();
Type listType = new TypeToken<List<CatalogueDetail>>(){}.getType();
ArrayList<CatalogueDetail> catalogueDetailslist = (List<CatalogueDetail>) gson.fromJson(jsonOutput, listType);
答案 0 :(得分:0)
以下是Pojo课程的示例:
class List{
CatalogueDetail[] data;
}
class CatalogueDetail{
int id;
int parent_id;
String catalogue_code;
String catalogue_name;
String image;
int image_code;
String price;
String description;
String color;
String fabric;
String innner;
String fabric_top;
String fabric_bottom;
String fabric_dupatta;
String dupatta;
String size;
String length;
String work;
String style;
String occasion;
String stitch_unstitch;
String blouse;
String date;
String main_category;
String sub_category;
}
//Use this method to get data from your json
gson.fromJson(json, List.class);
答案 1 :(得分:0)
根据您粘贴的json,您需要创建两个java类并将其转换为Example.java
Example ex = gson.fromJson(datastring, Example.class);
这两个课程如下。
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Example {
@SerializedName("data")
@Expose
private List<Datum> data = new ArrayList<Datum>();
}
-----------------------------------com.example.Datum.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Datum {
@SerializedName("id")
@Expose
private String id;
@SerializedName("parent_id")
@Expose
private String parentId;
@SerializedName("catalogue_code")
@Expose
private String catalogueCode;
@SerializedName("catalogue_name")
@Expose
private String catalogueName;
@SerializedName("image")
@Expose
private String image;
@SerializedName("image_code")
@Expose
private String imageCode;
@SerializedName("price")
@Expose
private String price;
@SerializedName("description")
@Expose
private String description;
@SerializedName("color")
@Expose
private String color;
@SerializedName("fabric")
@Expose
private String fabric;
@SerializedName("innner")
@Expose
private String innner;
@SerializedName("fabric_top")
@Expose
private String fabricTop;
@SerializedName("fabric_bottom")
@Expose
private String fabricBottom;
@SerializedName("fabric_dupatta")
@Expose
private String fabricDupatta;
@SerializedName("dupatta")
@Expose
private String dupatta;
@SerializedName("size")
@Expose
private String size;
@SerializedName("length")
@Expose
private String length;
@SerializedName("work")
@Expose
private String work;
@SerializedName("style")
@Expose
private String style;
@SerializedName("occasion")
@Expose
private String occasion;
@SerializedName("stitch_unstitch")
@Expose
private String stitchUnstitch;
@SerializedName("blouse")
@Expose
private String blouse;
@SerializedName("date")
@Expose
private String date;
@SerializedName("main_category")
@Expose
private String mainCategory;
@SerializedName("sub_category")
@Expose
private String subCategory;
}
答案 2 :(得分:0)
尝试以下代码......
import java.util.List;
public class TestPojo {
private List<DataEntity> data;
public void setData(List<DataEntity> data) {
this.data = data;
}
public List<DataEntity> getData() {
return data;
}
public static class DataEntity {
private String id;
private String parent_id;
private String catalogue_code;
private String catalogue_name;
private String image;
private String image_code;
private String price;
private String description;
private String color;
private String fabric;
private String innner;
private String fabric_top;
private String fabric_bottom;
private String fabric_dupatta;
private String dupatta;
private String size;
private String length;
private String work;
private String style;
private String occasion;
private String stitch_unstitch;
private String blouse;
private String date;
private String main_category;
private String sub_category;
public void setId(String id) {
this.id = id;
}
public void setParent_id(String parent_id) {
this.parent_id = parent_id;
}
public void setCatalogue_code(String catalogue_code) {
this.catalogue_code = catalogue_code;
}
public void setCatalogue_name(String catalogue_name) {
this.catalogue_name = catalogue_name;
}
public void setImage(String image) {
this.image = image;
}
public void setImage_code(String image_code) {
this.image_code = image_code;
}
public void setPrice(String price) {
this.price = price;
}
public void setDescription(String description) {
this.description = description;
}
public void setColor(String color) {
this.color = color;
}
public void setFabric(String fabric) {
this.fabric = fabric;
}
public void setInnner(String innner) {
this.innner = innner;
}
public void setFabric_top(String fabric_top) {
this.fabric_top = fabric_top;
}
public void setFabric_bottom(String fabric_bottom) {
this.fabric_bottom = fabric_bottom;
}
public void setFabric_dupatta(String fabric_dupatta) {
this.fabric_dupatta = fabric_dupatta;
}
public void setDupatta(String dupatta) {
this.dupatta = dupatta;
}
public void setSize(String size) {
this.size = size;
}
public void setLength(String length) {
this.length = length;
}
public void setWork(String work) {
this.work = work;
}
public void setStyle(String style) {
this.style = style;
}
public void setOccasion(String occasion) {
this.occasion = occasion;
}
public void setStitch_unstitch(String stitch_unstitch) {
this.stitch_unstitch = stitch_unstitch;
}
public void setBlouse(String blouse) {
this.blouse = blouse;
}
public void setDate(String date) {
this.date = date;
}
public void setMain_category(String main_category) {
this.main_category = main_category;
}
public void setSub_category(String sub_category) {
this.sub_category = sub_category;
}
public String getId() {
return id;
}
public String getParent_id() {
return parent_id;
}
public String getCatalogue_code() {
return catalogue_code;
}
public String getCatalogue_name() {
return catalogue_name;
}
public String getImage() {
return image;
}
public String getImage_code() {
return image_code;
}
public String getPrice() {
return price;
}
public String getDescription() {
return description;
}
public String getColor() {
return color;
}
public String getFabric() {
return fabric;
}
public String getInnner() {
return innner;
}
public String getFabric_top() {
return fabric_top;
}
public String getFabric_bottom() {
return fabric_bottom;
}
public String getFabric_dupatta() {
return fabric_dupatta;
}
public String getDupatta() {
return dupatta;
}
public String getSize() {
return size;
}
public String getLength() {
return length;
}
public String getWork() {
return work;
}
public String getStyle() {
return style;
}
public String getOccasion() {
return occasion;
}
public String getStitch_unstitch() {
return stitch_unstitch;
}
public String getBlouse() {
return blouse;
}
public String getDate() {
return date;
}
public String getMain_category() {
return main_category;
}
public String getSub_category() {
return sub_category;
}
}
}
使用Gson进行转换
TestPojo pj = new Gson().fromJson(<Your JSON String>, TestPojo.class);
希望这是有效的。