码迷,mamicode.com
首页 > Web开发 > 详细

json解析问题总结

时间:2016-04-15 18:17:31      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

一、使用gson解析json数据时,如果出现某一个属性为null的情况(且并无报错信息),首先检查json文件的属性名称和实体类的属性名称是否一致。

举例如下(正常情况,此处读取本地的一个json文件):

json文件

{"name":"Tim",
"age":"40",
"job":"basketball",
"hobby":"dai"}

解析代码如下

String jsonstring=FileUtil.readAssets(ctx,"tim.json");
		Gson g=new Gson();
		tim=g.fromJson(jsonstring, Tim.class);

实体类如下

package com.example.jsondemoerror;

public class Tim {
	private String name;
	private String age;
	private String job;
	private String hobby;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	@Override
	public String toString() {
		return "Tim [name=" + name + ", age=" + age + ", job=" + job
				+ ", hobby=" + hobby + "]";
	}
	

	
}

json文件的各属性名称和实体类的名称一致时,解析成功。

若出现名称不一致:

1、json文件的名称写错,实体类未错,比如hobby写为hobb,出现Tim [name=Tim, age=40, job=basketball, hobby=null]。

2、实体类属性名称写错,json未错,比如把hobby写为hobb,出现Tim [name=Tim, age=40, job=basketball, hobb=null]。

二、使用gson可以直接解析出实体类的集合。

json文件

 

{"name":"Tim",
"age":"40",
"job":"basketball",
"hobby":
[{"name":"sing","time":"1"},
{"name":"dance","time":"1"},
{"name":"swim","time":"1"},
{"name":"dai","time":"1"}]}

 

实体类如下

package com.example.jsondemoerror;

import java.util.List;

public class Tims {
    private String name;
    private String age;
    private String job;
    private List<Hobby> hobby;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public List<Hobby> getHobby() {
        return hobby;
    }
    public void setHobby(List<Hobby> hobby) {
        this.hobby = hobby;
    }
    @Override
    public String toString() {
        return "Tims [name=" + name + ", age=" + age + ", job=" + job
                + ", hobby=" + hobby + "]";
    }
    
    

    
}

解析代码

String jsonstring=FileUtil.readAssets(ctx,"tims.json");
        Gson g=new Gson();
        tims=g.fromJson(jsonstring, Tims.class);
        Log.d("JsonErrorActivity", tims.toString()+tims.getHobby().get(0).getName());

log内容

Tims [name=Tim, age=40, job=basketball, hobby=[Hobby [name=sing, time=1], Hobby [name=dance, time=1], Hobby [name=swim, time=1], Hobby [name=dai, time=1]]]sing

三、使用gson可以解析出实体类。

json文件

技术分享
{"name":"Tim",
"age":"40",
"job":"basketball",
"hobby":
{"name":"sing","time":"1"}
}
View Code

实体类

技术分享
package com.example.jsondemoerror;

public class Timsh {
    private String name;
    private String age;
    private String job;
    private Hobby hobby;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public Hobby getHobby() {
        return hobby;
    }
    public void setHobby(Hobby hobby) {
        this.hobby = hobby;
    }
    @Override
    public String toString() {
        return "Timsh [name=" + name + ", age=" + age + ", job=" + job
                + ", hobby=" + hobby + "]";
    }
    
    

    
}
View Code

解析代码

技术分享
String jsonstring=FileUtil.readAssets(ctx,"timsh.json");
        Gson g=new Gson();
        timsh=g.fromJson(jsonstring, Timsh.class);
        Log.d("JsonErrorActivity", timsh.toString()+timsh.getHobby().getName());
View Code

log内容

Timsh [name=Tim, age=40, job=basketball, hobby=Hobby [name=sing, time=1]]sing

 

 

 

json解析问题总结

标签:

原文地址:http://www.cnblogs.com/silenceshining/p/5396190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!