android gson 解析不规则数组

[{"older":{"olderId":3,"olderPhoto":"http://localhost/onecard/photos/20160826103903686.gif","olderName":"李老","age":60,"sex":"女"},"equId":1000001,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderId":1,"olderPhoto":"http://localhost/onecard/photos/20160808151252331.png","olderName":"刘老","age":63,"sex":"男"},"equId":1000002,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000003,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000005,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000006,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000007,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000008,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000010,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000020,"pCode":"CN-SZ-00002-3241","adminPhone":"18722574812"},
{"older":{"olderId":2,"olderPhoto":"http://localhost/onecard/photos/20160810143737724.png","olderName":"啊啊啊","age":65,"sex":"男"},"equId":1000026,"pCode":"CN-SZ-00002-3241","adminPhone":"18522039001"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000028,"pCode":"CN-SZ-00002-3241","adminPhone":"17801091244"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000029,"pCode":"CN-SZ-00002-3241","adminPhone":"17801091244"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000030,"pCode":"CN-SZ-00002-3241","adminPhone":"17801091244"},
{"older":{"olderPhoto":"http://localhost/onecard/photos/null"},"equId":1000031,"pCode":"CN-SZ-00002-3241","adminPhone":"13352077638"}]

这种格式的该怎么解析呢?

6个回答

我刚才用Gson解析了一遍,希望对你有帮助.

older对应的bean:
public class OlderBean {
private int age;
private int olderId;
private String olderName;
private String olderPhoto;
private String sex;

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int getOlderId() {
    return olderId;
}

public void setOlderId(int olderId) {
    this.olderId = olderId;
}

public String getOlderName() {
    return olderName;
}

public void setOlderName(String olderName) {
    this.olderName = olderName;
}

public String getOlderPhoto() {
    return olderPhoto;
}

public void setOlderPhoto(String olderPhoto) {
    this.olderPhoto = olderPhoto;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

}

User对应的bean:
public class UserBean {

private String adminPhone;
private int equId;
private OlderBean older;
private String pCode;

public String getAdminPhone() {
    return adminPhone;
}

public void setAdminPhone(String adminPhone) {
    this.adminPhone = adminPhone;
}

public int getEquId() {
    return equId;
}

public void setEquId(int equId) {
    this.equId = equId;
}

public OlderBean getOlder() {
    return older;
}

public void setOlder(OlderBean older) {
    this.older = older;
}

public String getpCode() {
    return pCode;
}

public void setpCode(String pCode) {
    this.pCode = pCode;
}

}

解析数据:
private List parseDataByGson(String result){
List userList = new ArrayList<>();
if (result != null && !TextUtils.isEmpty(result.toString())) {
try {

            userList = new Gson().fromJson(result, new TypeToken<ArrayList<UserBean>>() {
            }.getType());
            if (userList != null && userList.size() > 0) {

                for(UserBean user : userList){
                    Log.e("12345", user.getAdminPhone());
                    Log.e("12345", user.getOlder().getOlderName());
                    Log.e("12345", user.getOlder().getOlderPhoto());
                }
            }

        } catch (Exception e) {
            Log.e("1234", e.toString());

        }
    }
    return userList;
}

    android studio 的gradle导包:
    compile 'com.google.code.gson:gson:2.7'

这种 json格式很规范呀,用Gson很容易解析的

bgk6668
bgk6668 怎么写实体类呢?
接近 4 年之前 回复

 package ;
public class Older {
private int olderId;

private String olderPhoto;

private String olderName;

private int age;

private String sex;

public void setOlderId(int olderId){
this.olderId = olderId;
}
public int getOlderId(){
return this.olderId;
}
public void setOlderPhoto(String olderPhoto){
this.olderPhoto = olderPhoto;
}
public String getOlderPhoto(){
return this.olderPhoto;
}
public void setOlderName(String olderName){
this.olderName = olderName;
}
public String getOlderName(){
return this.olderName;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setSex(String sex){
this.sex = sex;
}
public String getSex(){
return this.sex;
}

}
=================================

package ;
public class Root {
private Older older;

private int equId;

private String pCode;

private String adminPhone;

public void setOlder(Older older){
this.older = older;
}
public Older getOlder(){
return this.older;
}
public void setEquId(int equId){
this.equId = equId;
}
public int getEquId(){
return this.equId;
}
public void setPCode(String pCode){
this.pCode = pCode;
}
public String getPCode(){
return this.pCode;
}
public void setAdminPhone(String adminPhone){
this.adminPhone = adminPhone;
}
public String getAdminPhone(){
return this.adminPhone;
}

}

这个就可以方便的让你查看JSON数据格式啦,

bgk6668
bgk6668 怎么解析呢
接近 4 年之前 回复
viewgroup
viewgroup 这个网址 http://www.bejson.com/json2javapojo/
接近 4 年之前 回复
   public class result
    {
        public Older older { get; set; }
        public int equId { get; set; }
        public string pCode { get; set; }
        public string adminPhone { get; set; }

    }

    public class Older
    {
        public int olderId { get; set; }
        public string olderPhoto { get; set; }
        public string olderName { get; set; }
        public int age { get; set; }
        public string sex { get; set; }

    }

               List<result> r = JsonConvert.DeserializeObject<List<result>>(str);
bgk6668
bgk6668 gson怎么解析,新手还不太明白
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐