【问题标题】:Unable to parse single json object无法解析单个 json 对象
【发布时间】:2015-06-28 12:05:03
【问题描述】:

我正在尝试解析 json 但无法获得解析结果。总是得到 null。下面是我的整个 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;

    }


}

【问题讨论】:

  • 您是否收到来自服务器的空响应?
  • 这里我是空的。在那时候。 postingDetailVO = gson.fromJson(jsonObject.toString(), PostingDetailVO.class); Log.e("", "WORKING BOY=="+postingDetailVO.getPhone_number());
  • jsonObject.toString() 很好。但是 PostingDetailVO.class);在日志中给我 null Log.e("", "WORKING BOY==" +postingDetailVO.getPhone_number());
  • 意味着你得到 jsonObject null。所以使用 Rest Client 来检查你是否通过和获取参数好吗?
  • 你有 posting_detail 作为它下面的所有字段的主要对象,所以你必须创建另一个只声明 PostingDetailVO 类对象的类。

标签: android json parsing gson getter-setter


【解决方案1】:

只需将另一个类命名为PostingDetail

public class PostingDetail implements Serializable{

 PostingDetailVO objectPostingDetailVO; 

}

然后代替

postingDetailVO = gson.fromJson(jsonObject.toString(), PostingDetailVO.class);

使用这个:

PostingDetail objPostingDetail = gson.fromJson(jsonObject.toString(), PostingDetail.class);

【讨论】:

  • 我仍然为空。在 log.postingDetail = gson.fromJson(jsonObject.toString(), PostingDetailVO.class); Log.e("", "WORKING BOY==" + postingDetail.getObjectPostingDetailVO().getAddress());
  • 我可以给你网址,你可以试试。我可以给你吗?
  • 嗨,我解决了我的最后一个解决方案。但在这里我面临数组转换为字符串的另一个问题。有谁能够帮助我。私有 ArrayList bannerGalleryId; bannerGalleryId = (ArrayList) getIntent().getExtras().getSerializable("bannerIdForGallery");但我想将此数组列表转换为画廊视图的字符串数组列表。
猜你喜欢
  • 2015-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
相关资源
最近更新 更多