java gson解析JSON
程序员文章站
2024-03-16 09:41:22
...
// json对象 转换成 json字符串
Gson gson = new Gson();
List<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 10; i++) {
Person p = new Person();
p.setName("name" + i);
p.setAge(i * 5);
persons.add(p);
}
String str = gson.toJson(persons);
// 结果: [{"name":"name0","age":0},{"name":"name1","age":5},{"name":"name2","age":10}]
// json字符串 转换成 json对象
List<Person> ps = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType());
for(int i = 0; i < ps.size() ; i++)
{
Person p = ps.get(i);
System.out.println(p.toString());
}
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。同XML一样是一种“传输格式”。JSON采用与编程语言无关的文本格式,便于数据传输、存储、交换。
## 封装类Attribute:
public class Attribute {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
另一个封装类GsonDataAnalysis:
public class GsonDataAnalysis {
private List<Attribute> list;
public List<Attribute> getList() {
return list;
}
public void setList(List<Attribute> list) {
this.list = list;
}
}
测试类Main方法:
/**
* Gson解析json数据
* @author ForeverLover
* @version 2015-04-19
*/
public class GsonTest {
public static void main(String[] args) {
String sTotalString = "{\"list\": [" +
"{\"id\":\"1\",\"name\":\"Tom\",\"age\":\"12\"}," +
"{\"id\":\"2\",\"name\":\"Marry\",\"age\":\"18\"}" +
"]}";
Gson gson = new Gson();
GsonDataAnalysis gda = gson.fromJson(
sTotalString, GsonDataAnalysis.class);
for(int i = 0; i < gda.getList().size(); i ++) {
System.out.print("ID号:" + gda.getList().get(i).getId() + " ");
System.out.print("姓 名:" + gda.getList().get(i).getName() + " ");
System.out.println("年 龄:" + gda.getList().get(i).getAge());
}
}
}
注:在使用Gson类的时候在工程中要添加Gson.jar包。
下面给大家介绍java 解析json格式数据
有时候可能会用到json格式进行数据的传输,那么怎么把接收到的数据解析出来呢? 下面介绍两种解析json数据的方法:
1、通过谷歌的Gson来进行解析:
json数据:sTotalString = {“message”:“success”,“result”:[{“surveyid”:“1”,“surveyname”:“B”}{surveyid":“2”,“surveyname”:“C”}]};
两个VO类:
public class SurveyVO {
private String surveyId;
private String surveyName;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getSurveyId() {
return surveyId;
}
public void setSurveyId(String surveyId) {
this.surveyId = surveyId;
}
public String getSurveyName() {
return surveyName;
}
public void setSurveyName(String surveyName) {
this.surveyName = surveyName;
}
}
public class SurveyListVO {
private String message;
private List<SurveyVO> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<SurveyVO> getResult() {
return result;
}
public void setResult(List<SurveyVO> result) {
this.result = result;
}
}
json格式转换成object类型:
public class fromGson() {
sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]};
Gson gson = new Gson();
SurveyListVO surveyListVO = gson.fromJson(sTotalString,
SurveyListVO.class);
for (int i = 0; i < surveyListVO.getResult().size(); i++) {
System.out.print(surveyListVO.getResult().get(i)
.getSurveyId());
print: 12
System.out.print(surveyListVO.getResult().get(i)
.getSurveyName());
print: B///C
System.out.print(surveyListVO.getMessage());
}
}
2、通过json-org.jar包进行解析:
json数据:sTotalString = {“message”:“success”,“result”:[{“surveyid”:“1”,“surveyname”:“B”}{surveyid":“2”,“surveyname”:“C”}]};
一个VO类:
public class SurveyVO {
private String surveyId;
private String surveyName;
public String getSurveyId() {
return surveyId;
}
public void setSurveyId(String surveyId) {
this.surveyId = surveyId;
}
public String getSurveyName() {
return surveyName;
}
public void setSurveyName(String surveyName) {
this.surveyName = surveyName;
}
}
json格式转换成object类型
public class fromJson() {
sTotalString = {"message":"success","result":[{"surveyid":"1","surveyname":"B"}{"surveyid":"2","surveyname":"C"}]};
JSONObject json;
try {
json = new JSONObject(sTotalString);
JSONArray results = json.getJSONArray("result");
for (int i = 0; i < results.length(); i++) {
SurveyVO surveyVO = new SurveyVO();
JSONObject result = results.getJSONObject(i);
System.out.println(result.getString("surveyId")+" "+result.getString("surveyName"));
surveyVO.setSurveyId(result.getString("surveyId"));
surveyVO.setSurveyName(result.getString("surveyName"));
surveyVOList.add(surveyVO);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
原创作者路径:http://www.zzvips.com/article/55855.html
推荐阅读
-
Android中Gson解析json数据使用@SerializedName注解与java对象不匹配的字段
-
FastJson解析JSON字符串以中文命名的key值和JavaBean字段冲突的解决
-
Spark Stuctured Streaming 解析字段不固定的JSON
-
Gson实现JSON字段过滤
-
java gson解析JSON
-
解决:Gson解析java实体类字段中包含json或其他值报错
-
使用GSON对带泛型的对象解析
-
Gson解析:将Json字段映射到不同名的Java字段上以及将多个Json字段映射到单个Java字段
-
Gson字段解析失败兼容(gson-plugin)
-
gson实体转json空字段处理