1、json的依赖包
json的依赖包共有6个:json-lib.jar、ezmorph.jar、commons-lang.jar、commons-beanutils.jar、commons-collections.jar、commons-logging.jar
2、下载地址
(1)json-lib-2.4-jdk15.jar
https://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/
(2)ezmorph-1.0.6.jar
https://sourceforge.net/projects/ezmorph/files/ezmorph/ezmorph-1.0.6/
(3)commons-lang-2.6.jar
http://commons.apache.org/proper/commons-lang/download_lang.cgi
(4)commons-beanutils-1.9.3.jar
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
(5)commons-collections-3.2.2.jar
http://commons.apache.org/collections/download_collections.cgi
(6)commons-logging-1.2.jar
http://commons.apache.org/proper/commons-logging/download_logging.cgi
注意:上述jar版本能够解析数据,如果换了其他版本可能会出现问题,如果需要相应的jar包可以从下述的github下载。
3、Json解析实例
github地址:
https://github.com/MasonYyp/analysisJson.git
//实体
package analysisJson;
public class Person {
public String name = null;
public int age = 0;
public String sex = null;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
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 analysisJson;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class AnalysisJson {
private String data = null;
private void setData(String data) {
this.data = data;
}
private String getData() {
return this.data;
}
// Analysis data of json
public void analysisData(String data) {
//new object of json
JSONObject jsonObject = new JSONObject();
jsonObject = JSONObject.fromObject(data);
JSONArray persons = jsonObject.getJSONArray("persons");
JSONObject personObject = new JSONObject();
Person person=new Person();
for(int i=0; i<persons.size();i++) {
/*
// First method, convert entity of class
JSONObject jsonObjectPerson = JSONObject.fromObject(persons.getString(i));
person = (Person) JSONObject.toBean(jsonObjectPerson, Person.class);
System.out.println(person.age);
*/
// Second method, direct call method
personObject = persons.getJSONObject(i);
System.out.println(personObject.getString("name"));
System.out.println(personObject.getInt("age"));
}
}
// Convert the entity of data
public void convertEntity() {
String json = "{\"person\":{\"name\":\"zhaoliu\",\"age\":\"30\",\"sex\":\"man\"}}";
JSONObject jsonObject = JSONObject.fromObject(json);
//First, you must convert the json to string
String stringPerson = jsonObject.getString("person");
//Then, convert the string to JSONObject,otherwise error !
JSONObject jsonObjectPerson = JSONObject.fromObject(stringPerson);
Person person = (Person) JSONObject.toBean(jsonObjectPerson, Person.class);
System.out.println(person.age);
}
// Generate the data of json
public void generateJson() {
// Set the list
ArrayList<Person> persons = new ArrayList<Person>();
Person person = new Person();
person.setName("sunlin");
person.setAge(10);
person.setSex("man");
persons.add(person);
person.setName("zhaowu");
person.setAge(20);
person.setSex("woman");
persons.add(person);
// Generate the list of json
JSONObject jsonObject = new JSONObject();
jsonObject.put("persons", persons);
System.out.println(jsonObject.toString());
}
public static void main(String[] args) {
AnalysisJson aj = new AnalysisJson();
//Json data
String data = "{\"persons\":[{\"name\":\"zhangsan\",\"age\":\"10\",\"sex\":\"man\"},"
+ "{\"name\":\"lisi\",\"age\":\"20\",\"sex\":\"woman\"},"
+ "{\"name\":\"wangwu\",\"age\":\"30\",\"sex\":\"man\"}]}";
// Print the data of json
System.out.println(data);
// Analysis the data of json
aj.setData(data);
aj.analysisData(aj.getData());
/*
// Convert the entity
aj.convertEntity();
*/
// Generate the data of json
aj.generateJson();
}
}
1万+

被折叠的 条评论
为什么被折叠?



