我正在尝试解析json但无法获得解析结果。总是得到null.Below是我的整个json与单个对象。
{
"posting_detail": {
"posting_id": "14",
"posting_title": "LuLu Last Offer",
"posting_desc": "dqwewqewe",
"mobile_number": "2344234234",
"phone_number": "34234324",
"address": "fefdsfds",
"city_name": "Abu Dhabi",
"created_on": "2015-06-22 14:55:05",
"normal_price": null,
"images": [
{
"photo_img": "xyz/images/posting/IMG_1212121219.jpg"
},
{
"photo_img": "xyzm/images/posting/IMG_1212121220.jpg"
}
]
}
}
这是数据集类。
package com.iptikarpromotion.vo;
import java.io.Serializable;
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class PostingDetailVO implements Serializable {
@SerializedName("posting_id")
private String posting_id;
@SerializedName("posting_title")
private String posting_title;
@SerializedName("posting_desc")
private String posting_desc;
@SerializedName("mobile_number")
private String mobile_number;
@SerializedName("phone_number")
private String phone_number;
@SerializedName("address")
private String address;
@SerializedName("city_name")
private String city_name;
@SerializedName("created_on")
private String created_on;
@SerializedName("normal_price")
private String normal_price;
@SerializedName("images")
private ArrayList<ImagesVO> images;
public PostingDetailVO() {
// TODO Auto-generated constructor stub
}
public PostingDetailVO(String posting_id, String posting_title, String posting_desc, String mobile_number, String phone_number, String address, String city_name, String created_on) {
this.posting_id = posting_id;
this.posting_title = posting_title;
this.posting_desc = posting_desc;
this.mobile_number = mobile_number;
this.phone_number = phone_number;
this.address = address;
this.city_name = city_name;
this.created_on = created_on;
}
public String getPosting_id() {
return posting_id;
}
public void setPosting_id(String posting_id) {
this.posting_id = posting_id;
}
public String getPosting_title() {
return posting_title;
}
public void setPosting_title(String posting_title) {
this.posting_title = posting_title;
}
public String getPosting_desc() {
return posting_desc;
}
public void setPosting_desc(String posting_desc) {
this.posting_desc = posting_desc;
}
public String getMobile_number() {
return mobile_number;
}
public void setMobile_number(String mobile_number) {
this.mobile_number = mobile_number;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
public String getCreated_on() {
return created_on;
}
public void setCreated_on(String created_on) {
this.created_on = created_on;
}
public String getNormal_price() {
return normal_price;
}
public void setNormal_price(String normal_price) {
this.normal_price = normal_price;
}
public ArrayList<ImagesVO> getImages() {
return images;
}
public void setImages(ArrayList<ImagesVO> images) {
this.images = images;
}
}
其他图片数据集类
public class ImagesVO implements Serializable {
@SerializedName("photo_img")
private String photo_img;
public String getPhoto_img() {
return photo_img;
}
public void setPhoto_img(String photo_img) {
this.photo_img = photo_img;
}
}
这是解析器方法。回应没有错。但只有问题无法解析整个json。
public static PostingDetailVO getPostingDetail(String response){
PostingDetailVO postingDetailVO = new PostingDetailVO();
if (response !=null) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject !=null && jsonObject.has(KEY_POSTING_DETAIL)) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
postingDetailVO = gson.fromJson(jsonObject.toString(), PostingDetailVO.class);
Log.e("", "WORKING BOY==" + postingDetailVO.getPhone_number());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return postingDetailVO;
}
}
答案 0 :(得分:0)
只需将另一个类命名为PostingDetail
public class PostingDetail implements Serializable{
PostingDetailVO objectPostingDetailVO;
}
然后代替
postingDetailVO = gson.fromJson(jsonObject.toString(), PostingDetailVO.class);
使用此:
PostingDetail objPostingDetail = gson.fromJson(jsonObject.toString(), PostingDetail.class);