我如何为它制作POJO类并从这个POJO类中获取价值

时间:2015-09-15 12:25:26

标签: android json pojo android-webservice

嗨我从服务器获得了JSON响应,如下所示。 我想知道如何在我的班级中将这个JSON类用作带有POJO的GSON。 我无法从帖子中获得价值。

我如何为它制作POJO类并从这个POJO类中获取价值。 任何人都可以从中创建POJO类,我将在我的应用程序中访问它。

先谢谢你。

 {  
"offers":[  
    {  
        "post":{  
            "of_id":"54",
            "of_title":"chocolate offer",
            "of_store_id":"46",
            "of_desc":"new arrivals",
            "st_logo":"no",
            "st_name":"chocolate store",
            "of_start":"23-2-2015",
            "of_end":"23-9-2015",
            "of_category_id":"9",
            "of_sub_category_id":"19,21"
        }
    },
    {  
        "post":{  
            "of_id":"55",
            "of_title":"test offer offer",
            "of_store_id":"47",
            "of_desc":"new test offer",
            "st_logo":"no",
            "st_name":"test",
            "of_start":"25-2-2015",
            "of_end":"25-9-2015",
            "of_category_id":"10",
            "of_sub_category_id":"36,37"
        }
    },
    {  
        "post":{  
            "of_id":"122",
            "of_title":"asd",
            "of_store_id":"112",
            "of_desc":"asd",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"01-09-2015",
            "of_end":"01-10-2015",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"61",
            "of_title":"latest collection",
            "of_store_id":"53",
            "of_desc":"get offers on our new collection",
            "st_logo":"678277054.jpg",
            "st_name":"central mall",
            "of_start":"9-3-2015",
            "of_end":"16-10-2015",
            "of_category_id":"9",
            "of_sub_category_id":"12,19,20,21"
        }
    },
    {  
        "post":{  
            "of_id":"72",
            "of_title":"Diwali Dhamaka",
            "of_store_id":"78",
            "of_desc":"Diwali dhamaka",
            "st_logo":"no",
            "st_name":"Riva Electronics",
            "of_start":"10-8-2015",
            "of_end":"25-11-2015",
            "of_category_id":"13",
            "of_sub_category_id":"9,13,14,15"
        }
    },
    {  
        "post":{  
            "of_id":"109",
            "of_title":"Buy 1 Get 1 Free",
            "of_store_id":"116",
            "of_desc":"Lace Border",
            "st_logo":"storeProfile103258.jpg",
            "st_name":"Pritam Lace",
            "of_start":"31-08-2015",
            "of_end":"30-11-2015",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"132",
            "of_title":"Chair",
            "of_store_id":"120",
            "of_desc":"Poster Chair",
            "st_logo":"storeProfile132640.jpg",
            "st_name":"Home Decora",
            "of_start":"02-09-2015",
            "of_end":"02-12-2015",
            "of_category_id":"14",
            "of_sub_category_id":"24"
        }
    },
    {  
        "post":{  
            "of_id":"130",
            "of_title":"Red Pan",
            "of_store_id":"116",
            "of_desc":"Designer Lace Border",
            "st_logo":"storeProfile103258.jpg",
            "st_name":"Pritam Lace",
            "of_start":"02-09-2015",
            "of_end":"05-12-2015",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"123",
            "of_title":"500 meter lace",
            "of_store_id":"116",
            "of_desc":"Designer Lace Border",
            "st_logo":"storeProfile103258.jpg",
            "st_name":"Pritam Lace",
            "of_start":"01-09-2015",
            "of_end":"08-12-2015",
            "of_category_id":"10",
            "of_sub_category_id":"37"
        }
    },
    {  
        "post":{  
            "of_id":"133",
            "of_title":"PHP Developer",
            "of_store_id":"54",
            "of_desc":"Experiance",
            "st_logo":"1682661683.jpg",
            "st_name":"Technotechindia 2",
            "of_start":"02-09-2015",
            "of_end":"09-12-2015",
            "of_category_id":"13",
            "of_sub_category_id":"14"
        }
    },
    {  
        "post":{  
            "of_id":"131",
            "of_title":"Mindi Fool",
            "of_store_id":"116",
            "of_desc":"Lace",
            "st_logo":"storeProfile103258.jpg",
            "st_name":"Pritam Lace",
            "of_start":"02-09-2015",
            "of_end":"12-12-2015",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"108",
            "of_title":"Computer ",
            "of_store_id":"112",
            "of_desc":"Hard Disk",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"31-08-2015",
            "of_end":"29-12-2015",
            "of_category_id":"13",
            "of_sub_category_id":"14"
        }
    },
    {  
        "post":{  
            "of_id":"98",
            "of_title":"Jaydip",
            "of_store_id":"112",
            "of_desc":"Godhani",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"31-08-2015",
            "of_end":"31-01-2016",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"53",
            "of_title":"new offer",
            "of_store_id":"28",
            "of_desc":"new offer",
            "st_logo":"no",
            "st_name":"abc",
            "of_start":"23-2-2015",
            "of_end":"2-3-2016",
            "of_category_id":"10",
            "of_sub_category_id":"37"
        }
    },
    {  
        "post":{  
            "of_id":"99",
            "of_title":"Dress",
            "of_store_id":"112",
            "of_desc":"Ethics",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"31-08-2015",
            "of_end":"31-08-2016",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"118",
            "of_title":"jaydip",
            "of_store_id":"112",
            "of_desc":"jaydip",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"01-09-2015",
            "of_end":"01-09-2016",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"119",
            "of_title":"godhanini",
            "of_store_id":"112",
            "of_desc":"godhani",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"01-09-2015",
            "of_end":"01-09-2016",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"124",
            "of_title":"45 Meter Lace",
            "of_store_id":"119",
            "of_desc":"10 Peace ",
            "st_logo":"storeProfile103606.jpg",
            "st_name":"Murlidhar Fashion",
            "of_start":"01-09-2015",
            "of_end":"01-09-2016",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"101",
            "of_title":"Jaydip",
            "of_store_id":"112",
            "of_desc":"fhihy",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"04-09-2015",
            "of_end":"04-09-2016",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"139",
            "of_title":"xyz",
            "of_store_id":"112",
            "of_desc":"xyz",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"14-09-2015",
            "of_end":"14-09-2016",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"140",
            "of_title":"kkk",
            "of_store_id":"112",
            "of_desc":"kkk",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"14-09-2015",
            "of_end":"14-09-2016",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"136",
            "of_title":"Buy 3 Get 1 Free",
            "of_store_id":"127",
            "of_desc":"Only Jens",
            "st_logo":"storeProfile073447.jpg",
            "st_name":"Fashion",
            "of_start":"14-09-2015",
            "of_end":"14-09-2017",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"138",
            "of_title":"abc",
            "of_store_id":"112",
            "of_desc":"bc",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"14-09-2015",
            "of_end":"14-09-2017",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"102",
            "of_title":"Jatin",
            "of_store_id":"112",
            "of_desc":"Gosha",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"04-09-2015",
            "of_end":"04-09-2018",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    },
    {  
        "post":{  
            "of_id":"137",
            "of_title":"Sale Harshit",
            "of_store_id":"127",
            "of_desc":"Harshit is Saled",
            "st_logo":"storeProfile073447.jpg",
            "st_name":"Fashion",
            "of_start":"14-09-2015",
            "of_end":"14-09-2019",
            "of_category_id":"10",
            "of_sub_category_id":"36"
        }
    },
    {  
        "post":{  
            "of_id":"70",
            "of_title":"offer",
            "of_store_id":"58",
            "of_desc":"100% off",
            "st_logo":"no",
            "st_name":"ABC",
            "of_start":"31-7-2015",
            "of_end":"31-7-2020",
            "of_category_id":"9",
            "of_sub_category_id":"12,19,20,21"
        }
    },
    {  
        "post":{  
            "of_id":"121",
            "of_title":"dgf",
            "of_store_id":"112",
            "of_desc":"sdfg",
            "st_logo":"storeProfile104923.jpg",
            "st_name":"Harshit",
            "of_start":"14-09-2015",
            "of_end":"14-01-2028",
            "of_category_id":"9",
            "of_sub_category_id":"12"
        }
    }
]

}

这里我的POJO课我无法获得价值从中我如何制作它,我可以用来访问价值并在我的应用程序中使用它。

     package objects;

     import com.google.gson.annotations.SerializedName;

      import java.util.List;

/**
 * Created by Technotech on 9/15/2015.
 */


    public class OffersStatus {

    @SerializedName("offers")
    public List<PostData> postDataList;



    public class PostData{

        @SerializedName("of_id")
        public String of_id;

        @SerializedName("of_title")
        public String of_title;

        @SerializedName("of_store_id")
        public String of_store_id;

        @SerializedName("of_desc")
        public String of_desc;

        @SerializedName("st_logo")
        public String st_logo;

        @SerializedName("st_name")
        public String st_name;

        @SerializedName("of_start")
        public String of_start;

        @SerializedName("of_end")
        public String of_end;

        @SerializedName("of_category_id")
        public String of_category_id;

        @SerializedName("of_sub_category_id")
        public String of_sub_category_id;
    }
    }

1 个答案:

答案 0 :(得分:0)

class PostData
{
  @SerializedName("of_id")
public String of_id;

@SerializedName("of_title")
public String of_title;

@SerializedName("of_store_id")
public String of_store_id;

@SerializedName("of_desc")
public String of_desc;

@SerializedName("st_logo")
public String st_logo;

@SerializedName("st_name")
public String st_name;

@SerializedName("of_start")
public String of_start;

@SerializedName("of_end")
public String of_end;

@SerializedName("of_category_id")
public String of_category_id;

@SerializedName("of_sub_category_id")
public String of_sub_category_id;

  public PostData(){}

 public PostData(int id, String title, int store_id,String desc,String st_logo,String st_name,String start, String end, int category_id,
  int sub_category_id)
  {
     this.of_id = id;
     this.of_title= title;
     this.of_store_id=store_id;
     this.of_desc=desc;
     this.st_logo=st_logo;
     this.st_name=st_name;
     this.of_start=start;
     this.of_end=end;
     this.of_category_id=category_id;
     this.of_sub_category_id=sub_category_id;
  }

  void setId(int id){this.of_id=id;}
  void setTitle(String title){this.of_title=title;}
  ....//do this for all the class variables
  int getId(){return this.of_id;}
  String getTitle(){return this.of_title;}
  ....//do this for all the class variables
}

//您可以在列表中添加PostData

 ArrayList<PostData> offerList = new ArrayList<PostData>();

 JSONObject job = new JSONObject(jsonResponseString);
 JSONArray jarr = job.getJSONArray("offers");
 for(int i=0;i<jarr.length();i++)
{
   JSONObject post = jarr.getJSONObject(i);
   PostData offer = new PostData();
   offer.setId(post.getJSONObject("of_id");
   offer.setTitle(post.getJSONObject("of_title");
   //...set all the json object

  offerList.add(offer);
}
相关问题