package com.handson.json;
import net.sf.json.*;
import java.util.*;
/**
* 将java的数据或对象转变成js可以用的数据或对象(json 服务器端实例)
*
* @author brj
*
*/
public class JSONTest {
/**
* 将Java中的数组转变成json中的数组
*/
public static void arrayToJson() {
String[] kkname = { "CCED", "WPS", "WORD" };
JSONArray jr = JSONArray.fromObject(kkname);
System.out.println(jr);
System.out.println("=========================================");
}
/**
* 将java的List中存的对象,转换成json中的对象数组
*/
public static void listToJson() {
List<Book> bklist = new ArrayList<Book>();
bklist.add(new Book("a1", "b", "111"));
bklist.add(new Book("a2", "b", "111"));
bklist.add(new Book("a3", "精通JAVA", "111"));
JSONArray jr = JSONArray.fromObject(bklist);
System.out.println(jr);
System.out.println("=========================================");
}
/**
* 将json中的对象数组,转变成java中的List
*/
public static void createJson() {
JSONArray jn = JSONArray
.fromObject("[{name:'JAVA程序设计',isbn:'885596-99',author:'小东'},"
+ "{name:'Thinking in Java', isbn:'123456', author:'Bruce'},"
+ "{name:'VC++', isbn:'7-123-515', author:'Liming'}]");
// 注意上面的字符串中,开头和结尾带 [ 或 ]
// List a = (List) JSONArray.toList(jn, Book.class);//这种也可以,只是不推荐使用了
List a = (List) JSONArray.toCollection(jn, Book.class);
Book book = null;
for (Iterator it = a.iterator(); it.hasNext();) {
book = (Book) it.next();
System.out.println(book.getName() + " - " + book.getAuthor()
+ " - " + book.getName());
}
System.out.println("=========================================");
}
/**
* map --> json
*/
public static void MapToJson() {
Map map = new HashMap();
map.put("name", "小裴");
map.put("book", new Book("K语言程序设计", "李", "55588"));
map.put("shoBookName", "function(){alert('测试')}");
JSONObject jo = JSONObject.fromObject(map);
System.out.println(jo);
System.out.println("=========================================");
}
/**
* bean --> json
*/
public static void beanToJson() {
JSONObject jo = JSONObject.fromObject(new Book("1", "3", "ABCd"));
System.out.println(jo);
System.out.println("=========================================");
}
/**
* json --> bean
*/
public static void jsonToBean() {
JSONObject jo = JSONObject
.fromObject("{name:'C++程序设计',isbn:'885596-989',author:'小东'}");
Book book = (Book) JSONObject.toBean(jo, Book.class);
System.out.println(book.getAuthor() + "-" + book.getName());
System.out.println("=========================================");
}
/**
* bean --> json
*/
public static void beanToJson2() {
Book book = new Book("isbn-1111", "Thinking in java", "Bruce");
Publisher publisher = new Publisher("1001", "中国机械工业出版社", "北京",
"010-111111");
book.setPublisher(publisher);
// JSONObject jo = JSONObject.fromObject(book);
// //{"author":"Bruce","isbn":"isbn-1111","name":"Thinking in
// java","publisher":{"address":"北京","code":"1001","name":"中国机械工业出版社","telephone":"010-111111"}}
JSONArray jo = JSONArray.fromObject(book); // [{"author":"Bruce","isbn":"isbn-1111","name":"Thinking
// in
// java","publisher":{"address":"北京","code":"1001","name":"中国机械工业出版社","telephone":"010-111111"}}]
System.out.println(jo);
System.out.println("=========================================");
}
/**
* json --> bean
*/
public static void jsonToBean2() {
// String json = "[{'author':'Bruce','isbn':'isbn-1111','name':'Thinking
// in
// java','publisher':{'address':'北京','code':'1001','name':'中国机械工业出版社','telephone':'010-111111'}}]";
String json = "{'author':'Bruce','isbn':'isbn-1111','name':'Thinking in java','publisher':{'address':'北京','code':'1001','name':'中国机械工业出版社','telephone':'010-111111'}}";
JSONObject jo = JSONObject.fromObject(json);
// JSONArray jo = JSONArray.fromObject(json);
// List list = (List)jo.toCollection(jo, Book.class);
// Book book = (Book)list.get(0);
Book book = (Book) JSONObject.toBean(jo, Book.class);
System.out.println(book.getName());
System.out.println(book.getPublisher().getName());
System.out.println("=========================================");
}
/**
* main to test method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
arrayToJson();
listToJson();
createJson();
MapToJson();
beanToJson();
jsonToBean();
beanToJson2();
jsonToBean2();
}
}
=====================================================
下面是用的Book类:
package com.handson.json;
public class Book {
private String name;
private String isbn;
private String author;
private Publisher publisher;
public Book() {
}
public Book(String isbn, String name, String author) {
this.isbn = isbn;
this.name = name;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Publisher getPublisher() {
return publisher;
}
public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
}