java利用json-lib操作json
原创
©著作权归作者所有:来自51CTO博客作者morris131的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.1. 下载json-lib.jar
http://sourceforge.net/projects/json-lib/files/json-lib/
1.2. Java对象转换为json
1.2.1. Map对象转换为json
Map2Json.java |
package json;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
public class Map2Json {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
"姓名", "张三");
"年龄", "22");
"性别", "男");
JSONArray jsonArray = JSONArray.fromObject(map);
System.out.println(jsonArray.toString());
}
}
|
运行结果:
[{"性别":"男","姓名":"张三","年龄":"22"}]
1.2.2. List转换为json对象
List2json.java |
package json;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
public class List2Json {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
}
}
|
结果:
["aa","bb","cc","dd"]
1.2.3. Java bean转为json对象
Person.java |
package json;
import java.util.Date;
public class Person {
private String name;
private String sex;
private int age;
private String address;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday
}
public Person(String name, String sex, int age, String address,
Date birthday) {
super();
this.name
this.sex
this.age
this.address
this.birthday
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
|
Bean2Json.java |
package json;
import java.util.Date;
import net.sf.json.JSONArray;
public class Bean2Json {
public static void main(String[] args) {
Person person = new Person();
person.setAddress("深圳福田");
person.setAge(22);
person.setBirthday(new Date());
person.setName("张三");
person.setSex("男");
JSONArray jsonArray = JSONArray.fromObject(person);
System.out.println(jsonArray.toString());
}
}
|
运行结果:
[{"address":"深圳福田","age":22,"birthday":{"date":27,"day":2,"hours":22,"minutes":47,"month":0,"seconds":22,"time":1422370042957,"timezoneOffset":-480,"year":115},"name":"张三","sex":"男"}]
1.3. Json转换为map对象
Json2Map.java |
package json;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import net.sf.json.JSONObject;
public class Json2Map {
public static void main(String[] args) {
"{'性别':'男','姓名':'张三','年龄':'22'}";
JSONObject object = JSONObject.fromObject(jsonStr);
Map<String,String> map = new HashMap<String,String>();
Set<String> keySet = object.keySet();
for (String key : keySet) {
map.put(key, object.getString(key));
}
Set<Entry<String,String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
|
运行结果:
性别:男
姓名:张三
年龄:22