json : json数据解析(一)
在项目中经常用到json格式的数据转换与解析,先前写过一些小例子,现在整理下,以备后用和帮助后来者。
言归正传:
使用到的jar包 :json-lib-2.4-jdk15.jar,当然你也可以用自己版本的,一般都没啥问题。
一、用于测试数据的类:
1.Student
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.json.gson; import java.util.List; /**
* gsonTest测试类 : 学生
*
* @author zhengze.su
*/
public class Student { private String name;
private String sex;
private int age;
private String address;
private List<Hobby> hobbyList;
private List<Course> courseList; /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the sex
*/
public String getSex() {
return sex;
} /**
* @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
} /**
* @return the age
*/
public int getAge() {
return age;
} /**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
} /**
* @return the address
*/
public String getAddress() {
return address;
} /**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
} /**
* @return the hobbyList
*/
public List<Hobby> getHobbyList() {
return hobbyList;
} /**
* @param hobbyList the hobbyList to set
*/
public void setHobbyList(List<Hobby> hobbyList) {
this.hobbyList = hobbyList;
} /**
* @return the courseList
*/
public List<Course> getCourseList() {
return courseList;
} /**
* @param courseList the courseList to set
*/
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
}
2.Hobby
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.json.gson; /**
* gsonTest测试类 : 爱好
*
* @author zhengze.su
*/
public class Hobby { private String hobbyName; /**
* @return the hobbyName
*/
public String getHobbyName() {
return hobbyName;
} /**
* @param hobbyName the hobbyName to set
*/
public void setHobbyName(String hobbyName) {
this.hobbyName = hobbyName;
}
}
3.Course
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.json.gson; /**
* gsonTest测试类 : 课程
*
* @author zhengze.su
*/
public class Course { private String teacherName;
private String courseName; /**
* @return the teacherName
*/
public String getTeacherName() {
return teacherName;
} /**
* @param teacherName the teacherName to set
*/
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
} /**
* @return the courseName
*/
public String getCourseName() {
return courseName;
} /**
* @param courseName the courseName to set
*/
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
4.Person
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.json.gson; /**
*
* @author Administrator
*/
public class Person { private String name;
private String age;
private String hobby; /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
} /**
* @return the age
*/
public String getAge() {
return age;
} /**
* @param age the age to set
*/
public void setAge(String age) {
this.age = age;
} /**
* @return the hobby
*/
public String getHobby() {
return hobby;
} /**
* @param hobby the hobby to set
*/
public void setHobby(String hobby) {
this.hobby = hobby;
}
}
5.模拟获取数据
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.json.gson; import java.util.ArrayList;
import java.util.List; /**
* 获取模拟数据
*
* @author Administrator
*/
public class GetDataUtil { /**
* 获取hobbyList
*
* @return
*/
public List<Hobby> getHobbyList() {
List<Hobby> hobbyList = new ArrayList<Hobby>();
Hobby hobby = null;
for (int i = 0; i <= 2; i++) {
hobby = new Hobby();
hobby.setHobbyName("爱好(" + i + 1 + ")");
hobbyList.add(hobby);
} return hobbyList;
} /**
* 获取courseList
*
* @return
*/
public List<Course> getCourseList() {
List<Course> courseList = new ArrayList<Course>();
Course course = null;
for (int i = 0; i <= 0; i++) {
course = new Course();
course.setCourseName("课程" + (i + 1));
course.setTeacherName("老师" + (i + 1));
courseList.add(course);
} return courseList;
} public List<Student> getStudentList() { List<Student> studentList = new ArrayList<Student>();
List<Hobby> hobbyList = this.getHobbyList();
List<Course> courseList = this.getCourseList();
Student student = null;
for (int i = 0; i <= 0; i++) {
student = new Student();
student.setName("学生" + (i + 1));
if (i % 2 == 0) {
student.setSex("男");
} else {
student.setSex("女");
}
student.setAge(i + 10);
student.setHobbyList(hobbyList);
student.setCourseList(courseList);
studentList.add(student);
} return studentList; }
}
二、gsonTest测试类 : 方法类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.json.gson; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig; /**
* gsonTest测试类 : 方法类
*
* @author zhengze.su
*/
public class GsonTest001 { // //获取模拟数据
// public void getData() {
// GetDataUtil dataUtil = new GetDataUtil();
// List<Student> studentList = dataUtil.getStudentList();
// System.out.println("studentList.size():"+studentList.size());
// } //1. list对象转换成json字符串
// 备注 :此处只是将 student 对象转换成了 json字符串,后面将利用此处的数据,将其转换为对应的对象
public void studentListToJson() {
GetDataUtil dataUtil = new GetDataUtil();
List<Student> studentList = dataUtil.getStudentList();
JSONArray jsonArray = JSONArray.fromObject(studentList);
System.out.println(jsonArray);
} //2. json字符串转list输出,类型为 String
public void jsonStrToList() {
String jsonStr = "[\"aa1\",\"bb2\",\"cc3\",\"dd4\"]";
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(jsonStr);
System.out.println("出来吧,皮卡丘:" + jsonArray);
List<String> strList = (List) JSONSerializer.toJava(jsonArray);
for (String s : strList) {
System.out.println("打印出来看一看吆:" + s);
}
} //////// json 字符串 转为 对象,例子比较简单,主要是区分一下不同的形式
/////// 关于此处提到了 JsonConfig() ,后面再写个关于其配置的小例子
//3. json字符串转对象,以 person类为例
public void testJsonList1() { //
String json = "[{'name':'ml','age':'24','hobby':'eat'},{'name':'aarn','age':'25','hobby':'play'},{'name':'su','age':'xx','hobby':'study'}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
List list = (List) JSONArray.toCollection(jsonarray, Person.class);//person类
Iterator ite = list.iterator();
while (ite.hasNext()) {
Person pp = (Person) ite.next();
System.out.println(pp.getName());
}
} //4. json字符串转对象,以 person类为例
public void testJsonList2() { //2
String json = "[{'name':'ml','age':'24','hobby':'eat'},{'name':'aarn','age':'25','hobby':'play'},{'name':'su','age':'xx','hobby':'study'}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
List list = (List) JSONArray.toList(jsonarray, Person.class);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Person ppp = (Person) iter.next();
System.out.println(ppp.getHobby());
}
} //5. json字符串转对象,以 person类为例
public void testJsonList3() { //
String json = "[{'name':'ml','age':'24','hobby':'eat'},{'name':'aarn','age':'25','hobby':'play'},{'name':'su','age':'xx','hobby':'study'}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
List list = JSONArray.toList(jsonarray, new Person(), new JsonConfig());
Iterator itera = list.iterator();
while (itera.hasNext()) {
Person pe = (Person) itera.next();
System.out.println(pe.getAge());
}
} //6. json字符串转对象,以 person类为例
public void testJsonList4() { //
String json = "[{'name':'su','age':'25','hobby':'play'},{'name':'wa','age':'24','hobby':'eat'},{'name':'s','age':'aa','hobby':'mm'}]";
JSONArray jsonarray = (JSONArray) JSONSerializer.toJSON(json);
System.out.println(jsonarray);
List list = (List) JSONSerializer.toJava(jsonarray);
for (Object obj : list) {
JSONObject jbj = JSONObject.fromObject(obj);
Person p = (Person) JSONObject.toBean(jbj, Person.class);
System.out.println(p.getName());
}
} //7. json字符串转对象,以 person类为例
public void testJsonList5() { //
String json = "[{'name':'su','age':'25','hobby':'play'},{'name':'wa','age':'24','hobby':'eat'},{'name':'s','age':'aa','hobby':'mm'}]";
JSONArray jsonarray = (JSONArray) JSONSerializer.toJSON(json);
System.out.println(jsonarray);
List list = (List) JSONSerializer.toJava(jsonarray);
Iterator ite = list.iterator();
while (ite.hasNext()) {
JSONObject jsonObject = JSONObject.fromObject(ite.next());
Person pp = (Person) JSONObject.toBean(jsonObject, Person.class);
System.out.println(pp.getName());
}
} public static void main(String[] args) {
GsonTest001 gsonTest001 = new GsonTest001();
// gsonTest001.getData();
// gsonTest001.studentListToJson();
// gsonTest001.jsonStrToList();
// gsonTest001.testJsonList1();
// gsonTest001.testJsonList2();
// gsonTest001.testJsonList3();
// gsonTest001.testJsonList4();
// gsonTest001.testJsonList5(); }
}
下一篇将代码注释中提到的未完成的部分,包括解析 对象存在多个属性(包含集合属性)、jsonConfig()的简单使用、某个童鞋给的一份json数据的情况(据说是挺复杂的),在后面的文章中一一分享给大家。
json : json数据解析(一)的更多相关文章
- Android之JSON格式数据解析
查看原文:http://blog.csdn.net/hantangsongming/article/details/42234293 JSON:JavaScript 对象表示法(JavaScript ...
- HttpClient 模拟发送Post和Get请求 并用fastjson对返回json字符串数据解析,和HttpClient一些参数方法的deprecated(弃用)的综合总结
最近在做一个接口调用的时候用到Apache的httpclient时候,发现引入最新版本4.5,DefaultHttpClient等老版本常用的类已经过时了,不推荐使用了:去官网看了一下在4.3之后就抛 ...
- html中通过js获取接口JSON格式数据解析以及跨域问题
前言:本人自学前端开发,一直想研究下js获取接口数据在html的实现,顺利地找到了获取数据的方法,但是有部分接口在调用中出现无法展示数据.经查,发现时跨域的问题,花费了一通时间,随笔记录下过程,以方便 ...
- SAX, JSON , DOM 数据解析
//解析:将特定数据格式(如:xml,json)中提取出来所需的内容 //SAX: Simply API for XML, xml解析的一种方式,逐行解析,读一行内容,取一行内容,速度慢,占用内存小, ...
- Json.Net 数据解析
参考资料: 随笔分类 - Json.Net系列
- Java数据解析---JSON
一.Java数据解析分为:XML解析和JSON解析 XML解析即是对XML文件中的数据解析,而JSON解析即对规定形式的数据解析,比XML解析更加方便 JSON解析基于两种结构: 1.键值对类型 { ...
- 通过Jquery中Ajax获取json文件数据
1. JSON(JavaScript Object Notation): javaScript对象表示法: 是存储和交换文本信息的语法,比xml更小,更快,更易解析. 2. JSON基本书写格式 : ...
- iOS开发网络篇-JSON文件的解析
一.什么是JSON数据 1.JSON的简单介绍 JSON:是一种轻量级的传输数据的格式,用于数据的交互 JSON是javascript语言的一个子集.javascript是个脚本语言(不需要编译),用 ...
- 数据解析(XML和JSON数据结构)
一 解析 二 XML数据结构 三 JSON 数据结构 一 解析 1 定义: 从事先规定好的格式中提取数据 解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照 ...
- C# 解析JSON格式数据
JSON简介 JSON(全称为JavaScript ObjectNotation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集.JSON采用完全独立于语言的文本格式,可 ...
随机推荐
- 绝不要在构造函数和析构过程中调用virtual函数
下面是一个用来塑模股市交易的类: derived的类的构造函数被调用,但是首先得调用基类Transaction的构造函数,但是在后面还得调用virrual函数,这个时候子类的对象的构造还没有完成,那么 ...
- leetcode第一刷_Permutation Sequence
这道题还挺好的,假设你的思路是每次生成一个全排列,然后累计到k次,那么停下来吧.肯定超时了亲.. 微软今年的笔试题里有一道类似的,我之前已经提到过了.是唯独0和1的字符串,求第k个排列是什么样子的.这 ...
- bzoj1458 士兵占据
1458: 士兵占据 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 685 Solved: 398 [Submit][Status][id=1458 ...
- Continuous Integration with Selenium
I have seen a lot of queries from people who basically want to know how to blend Selenium, Maven, an ...
- registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
本文转载至 http://bbs.csdn.net/topics/390889517 IOS8 PUSH解决方法 昨天晚上整理PUSH的东西,准备些一个教程,全部弄好之后,发现没有达到预期的效果,本以 ...
- EasyPusher手机直播推送是如何实现后台直播推送的
本文由EasyDarwin开源团队成员John提供:http://blog.csdn.net/jyt0551/article/details/52276062 EasyPusher Android是使 ...
- poj 2154 Color < 组合数学+数论>
链接:http://poj.org/problem?id=2154 题意:给出两个整数 N 和 P,表示 N 个珠子,N种颜色,要求不同的项链数, 结果 %p ~ 思路: 利用polya定理解~定理内 ...
- 九度OJ 1115:数字求和 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2396 解决:1507 题目描述: 给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,小于a的整数的和是多少? 输入: 输入一行 ...
- select version();desc mysql.user;
D:\wamp64\wamp\bin\mysql\mysql5.6.17\bin>mysql -hgoDev -uroot -ppasswordWarning: Using a password ...
- 简单老式Java对象 横切关注点 最小侵入性编程 声明式编程 避免强迫类继承和接口实现
Spring In Action data injection aspect-oriented programming Plain Old Java Object 依赖注入能让相互协作的软件组件保持松 ...