如何使用Retrofit解析动态json

时间:2015-10-11 20:53:02

标签: android json dynamic retrofit pojo

我用户改装1.9,我得到动态响应,我有数据数组,里面有动态数字和数据。

如何通过改造来解析它?

{
  "status": "Successful",
  "id": "0",
  "val": "0",
  "html": "",
  "err": "",
  "msg": "",
  "text": "",
  "data": {
    "1": {
      "oab_id": "",
      "email": "",
      "first_name": "",
      "last_name": "",
      "org_id": "",
      "org_unit_id": "",
      "job_title": "",
      "unit_number": "",
      "unit_name": "",
      "birthday": "",
      "country_prefix": "",
      "mobile_number": "",
      "active_status": "",
      "ldap_username": "",
      "account_id": "",
      "account_name": "",
      "display_name": "",
      "text_status": "",
      "account_type_id": "",
      "profile_photo_url": "",
      "profile_icon_url": ""
    },
      "last_name": "",
      "org_id": "",
      "org_unit_id": "",
      "job_title": "",
      "unit_number": "",
      "unit_name": "",
      "birthday": "",
      "country_prefix": "",
      "mobile_number": "",
      "active_status": "",
      "ldap_username": "",
      "account_id": "",
      "account_name": "",
      "display_name": "",
      "text_status": "",
      "account_type_id": "",
      "profile_photo_url": "",
      "profile_icon_url": ""
    },
    "2": {
      "oab_id": "",
      "email": "",
      ".......
    }
  },
}

我的改装界面

@FormUrlEncoded
    @POST(SocketDefines.Path.KEY_GET_OAB)
    void doSomething(@FieldMap Map<String, Object> params, Callback<OrganizationEntity> callback);

POJO

public class OrganizationEntity {

    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("val")
    @Expose
    private String val;
    @SerializedName("html")
    @Expose
    private String html;
    @SerializedName("err")
    @Expose
    private String err;
    @SerializedName("msg")
    @Expose
    private String msg;
    @SerializedName("text")
    @Expose
    private String text;
    @SerializedName("modified")
    @Expose
    private String modified;
    @SerializedName("redis")
    @Expose
    private String redis;

    @SerializedName("data")
    @Expose
    private ArrayList<Object> data = new ArrayList<>();

    public ArrayList<Object> getData() {
        return data;
    }

    public void setData(ArrayList<Object> data) {
        this.data = data;
    }

    /**
     *
     * @return
     * The status
     */
    public String getStatus() {
        return status;
    }

    /**
     *
     * @param status
     * The status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The val
     */
    public String getVal() {
        return val;
    }

    /**
     *
     * @param val
     * The val
     */
    public void setVal(String val) {
        this.val = val;
    }

    /**
     *
     * @return
     * The html
     */
    public String getHtml() {
        return html;
    }

    /**
     *
     * @param html
     * The html
     */
    public void setHtml(String html) {
        this.html = html;
    }

    /**
     *
     * @return
     * The err
     */
    public String getErr() {
        return err;
    }

    /**
     *
     * @param err
     * The err
     */
    public void setErr(String err) {
        this.err = err;
    }

    /**
     *
     * @return
     * The msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     *
     * @param msg
     * The msg
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     *
     * @return
     * The text
     */
    public String getText() {
        return text;
    }

    /**
     *
     * @param text
     * The text
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     *
     * @return
     * The modified
     */
    public String getModified() {
        return modified;
    }

    /**
     *
     * @param modified
     * The modified
     */
    public void setModified(String modified) {
        this.modified = modified;
    }

    /**
     *
     * @return
     * The redis
     */
    public String getRedis() {
        return redis;
    }

    /**
     *
     * @param redis
     * The redis
     */
    public void setRedis(String redis) {
        this.redis = redis;
    }

}

Picture from post man to show the structure of the json

public class Data {
    @SerializedName("oab_id")
    @Expose
    private String oabId;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("first_name")
    @Expose
    private String firstName;
    @SerializedName("last_name")
    @Expose
    private String lastName;
    @SerializedName("org_id")
    @Expose
    private String orgId;
    @SerializedName("org_unit_id")
    @Expose
    private String orgUnitId;
    @SerializedName("job_title")
    @Expose
    private String jobTitle;
    @SerializedName("unit_number")
    @Expose
    private String unitNumber;
    @SerializedName("unit_name")
    @Expose
    private String unitName;
    @SerializedName("birthday")
    @Expose
    private String birthday;
    @SerializedName("country_prefix")
    @Expose
    private String countryPrefix;
    @SerializedName("mobile_number")
    @Expose
    private String mobileNumber;
    @SerializedName("active_status")
    @Expose
    private String activeStatus;
    @SerializedName("ldap_username")
    @Expose
    private String ldapUsername;
    @SerializedName("account_id")
    @Expose
    private String accountId;
    @SerializedName("account_name")
    @Expose
    private String accountName;
    @SerializedName("display_name")
    @Expose
    private String displayName;
    @SerializedName("text_status")
    @Expose
    private String textStatus;
    @SerializedName("account_type_id")
    @Expose
    private String accountTypeId;
    @SerializedName("profile_photo_url")
    @Expose
    private String profilePhotoUrl;
    @SerializedName("profile_icon_url")
    @Expose
    private String profileIconUrl;

    /**
     *
     * @return
     * The oabId
     */
    public String getOabId() {
        return oabId;
    }

    /**
     *
     * @param oabId
     * The oab_id
     */
    public void setOabId(String oabId) {
        this.oabId = oabId;
    }

    /**
     *
     * @return
     * The email
     */
    public String getEmail() {
        return email;
    }

    /**
     *
     * @param email
     * The email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     *
     * @return
     * The firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     *
     * @param firstName
     * The first_name
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     *
     * @return
     * The lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     *
     * @param lastName
     * The last_name
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     *
     * @return
     * The orgId
     */
    public String getOrgId() {
        return orgId;
    }

    /**
     *
     * @param orgId
     * The org_id
     */
    public void setOrgId(String orgId) {
        this.orgId = orgId;
    }

    /**
     *
     * @return
     * The orgUnitId
     */
    public String getOrgUnitId() {
        return orgUnitId;
    }

    /**
     *
     * @param orgUnitId
     * The org_unit_id
     */
    public void setOrgUnitId(String orgUnitId) {
        this.orgUnitId = orgUnitId;
    }

    /**
     *
     * @return
     * The jobTitle
     */
    public String getJobTitle() {
        return jobTitle;
    }

    /**
     *
     * @param jobTitle
     * The job_title
     */
    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    /**
     *
     * @return
     * The unitNumber
     */
    public String getUnitNumber() {
        return unitNumber;
    }

    /**
     *
     * @param unitNumber
     * The unit_number
     */
    public void setUnitNumber(String unitNumber) {
        this.unitNumber = unitNumber;
    }

    /**
     *
     * @return
     * The unitName
     */
    public String getUnitName() {
        return unitName;
    }

    /**
     *
     * @param unitName
     * The unit_name
     */
    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

    /**
     *
     * @return
     * The birthday
     */
    public String getBirthday() {
        return birthday;
    }

    /**
     *
     * @param birthday
     * The birthday
     */
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    /**
     *
     * @return
     * The countryPrefix
     */
    public String getCountryPrefix() {
        return countryPrefix;
    }

    /**
     *
     * @param countryPrefix
     * The country_prefix
     */
    public void setCountryPrefix(String countryPrefix) {
        this.countryPrefix = countryPrefix;
    }

    /**
     *
     * @return
     * The mobileNumber
     */
    public String getMobileNumber() {
        return mobileNumber;
    }

    /**
     *
     * @param mobileNumber
     * The mobile_number
     */
    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    /**
     *
     * @return
     * The activeStatus
     */
    public String getActiveStatus() {
        return activeStatus;
    }

    /**
     *
     * @param activeStatus
     * The active_status
     */
    public void setActiveStatus(String activeStatus) {
        this.activeStatus = activeStatus;
    }

    /**
     *
     * @return
     * The ldapUsername
     */
    public String getLdapUsername() {
        return ldapUsername;
    }

    /**
     *
     * @param ldapUsername
     * The ldap_username
     */
    public void setLdapUsername(String ldapUsername) {
        this.ldapUsername = ldapUsername;
    }

    /**
     *
     * @return
     * The accountId
     */
    public String getAccountId() {
        return accountId;
    }

    /**
     *
     * @param accountId
     * The account_id
     */
    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    /**
     *
     * @return
     * The accountName
     */
    public String getAccountName() {
        return accountName;
    }

    /**
     *
     * @param accountName
     * The account_name
     */
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    /**
     *
     * @return
     * The displayName
     */
    public String getDisplayName() {
        return displayName;
    }

    /**
     *
     * @param displayName
     * The display_name
     */
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    /**
     *
     * @return
     * The textStatus
     */
    public String getTextStatus() {
        return textStatus;
    }

    /**
     *
     * @param textStatus
     * The text_status
     */
    public void setTextStatus(String textStatus) {
        this.textStatus = textStatus;
    }

    /**
     *
     * @return
     * The accountTypeId
     */
    public String getAccountTypeId() {
        return accountTypeId;
    }

    /**
     *
     * @param accountTypeId
     * The account_type_id
     */
    public void setAccountTypeId(String accountTypeId) {
        this.accountTypeId = accountTypeId;
    }

    /**
     *
     * @return
     * The profilePhotoUrl
     */
    public String getProfilePhotoUrl() {
        return profilePhotoUrl;
    }

    /**
     *
     * @param profilePhotoUrl
     * The profile_photo_url
     */
    public void setProfilePhotoUrl(String profilePhotoUrl) {
        this.profilePhotoUrl = profilePhotoUrl;
    }

    /**
     *
     * @return
     * The profileIconUrl
     */
    public String getProfileIconUrl() {
        return profileIconUrl;
    }

    /**
     *
     * @param profileIconUrl
     * The profile_icon_url
     */
    public void setProfileIconUrl(String profileIconUrl) {
        this.profileIconUrl = profileIconUrl;
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您的JSon格式错误,因为它不代表数组,而是代表关系对象。 就个人而言,我建议使用GSON将Json解析为特定模型,例如

private static RestAdapter restAdapter;
private static Gson gson;

gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.registerTypeAdapter(Date.class, Utils.dateDeserializer)
.create();

restAdapter = new RestAdapter.Builder()
.setEndpoint(AppConfig.END_POINT)
.setClient(new OkClient(new OkHttpClient()))
.setLogLevel(RestAdapter.LogLevel.FULL)
.setConverter(new GsonConverter(gson))
.build();

我的DateDeserializer util。

    public class Utils {



        public static JsonDeserializer<Date> dateDeserializer = new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(JsonElement json, Type typeOfT,
                                    JsonDeserializationContext context) throws JsonParseException {
                try{
                    if(json==null){
                        return null;
                    }
                    else{
                        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        String dateString = json.getAsString();
                        Date date = format.parse(dateString);
                        return date;
                    }
                }
                catch(ParseException e){
                    return null;
                }
            }
        };

    }


  final WebService service = restAdapter.create(WebService.class);
                service.doSomething(params, new Callback<OrganizationEntity>() {
                    @Override
                    public void success(OrganizationEntity callBackOrganizationEntity, Response response) {
                        if (callBackOrganizationEntity != null) {
                           //handle 
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        //handle error
                    }
                });

如果你有一个与Json匹配的正确SerializedName,你就不会对序列化有任何问题。 希望它可以帮到你。