日期:2014-05-16  浏览次数:20393 次

JSON-LIB 学习笔记
JSON –LIB  完成json字符串的自动生成
java 的 工具集  完成的功能就是对json字符串的处理 
配合使用AJAX 太happy了
Android…等用的都是

sid:332575101
title:”擦”
content:”我擦”

导包 
json.jar
ezmorph.jar
commons-lang.jar
commons-collections.jar
commons-beanutils.jar  (主要对bean   就是对java 类的工具集  就可以把一个person对象的属性全自动赋值  )
commons-logging.jar

这些包 都在struts2 框架里    lib 里


建个test  导入Junit  addlib  


1转换一个对象到字符串   名值对   一想就是map 
JSONObject .formObject(map).toString()    这个工具把map转成JSON字符串
/**
* {"parentId":88888,"itemId":9999,"itemText":"岳元璞"}
* 转换一个对象,到 json字符串
*/
@Test
public void testJson1(){
Map map = new HashMap();
map.put("parentId", 88888);
map.put("itemId", 9999);
map.put("itemText", "岳元璞");

String jsonStr = JSONObject.fromObject(map).toString();
System.out.println(jsonStr);
}
打印结果
{"parentId":8888,"itemText":"岳元璞","itemId":9999}

2转换成一个普通的对象
User     首先你的domain包里都有这个类
/**
* 转换一个普通Java对象
* User
* {"id":1111,"username":岳元璞,"password":1234}
*/
@Test
public void testJson2(){
User user = new User();
user.setId(1111);
user.setUsername("岳元璞");
user.setPassword("1234");

String jsonStr = JSONObject.fromObject(user).toString();
System.out.println(jsonStr);
}
{"id":1111,"username":"岳元璞","password":"1234"}

3从集合进行转换成JSON字符串
[{“id”: 1111,”userna”},{“id” : ,},{ },{ } ]
/**
* [{"id":1111,"username":"岳元璞","password":"1234"},{},{},{},{}]
*/
@Test
public void testJson3(){
List<User> userList = new ArrayList<User>();
for(int i=0;i<10;i++){
User user = new User();

user.setId(1111 + i);
user.setUsername("岳元璞" + i);
user.setPassword("1234" + i);
userList.add(user);
}
String jsonStr = JSONArray.fromObject(userList).toString();
System.out.println(jsonStr);
}
[{"id":1111,"username":"岳元璞0","password":"12340"},{"id":1112,"username":"岳元璞1","password":"12341"},{"id":1113,"username":"岳元璞2","password":"12342"},{"id":1114,"username":"岳元璞3","password":"12343"},{"id":1115,"username":"岳元璞4","password":"12344"},{"id":1116,"username":"岳元璞5","password":"12345"},{"id":1117,"username":"岳元璞6","password":"12346"},{"id":1118,"username":岳元璞7","password":"12347"},{"id":1119,"username":"岳元璞8","password":"12348"},{"id":1120,"username":"岳元璞9","password":"12349"}]
4Myeclipse Ctrl  shift +f  快捷键   格式化的
多对一 先把组弄出来 确保你有Group这个类
/**
* [{"id":1111,"username":"岳元璞","password":"1234"},{},{},{},{}]
*/
@Test
public void testJson4(){

Group group = new Group();
group.setId(123);
group.setName("通信093");

Person person = new Person();
person.setId(1111);
person.setUsername("岳元璞");
person.setPassword("123");
person.setGroup(group);


String jsonStr = JSONObject.fromObject(person).toString();
System.out.println(jsonStr);
}
{"id":1111,"username":"岳元璞","group":{"id":123,"name":"通信093"},"password":"123"}
5对象嵌套
{success:true,data:{id:1111,username:”mm”}}
/**
* {success:true,data:{id:111,username:" 岳元璞"}}
*/
@Test
public void testJson5(){
Map map = new HashMap();
map.put("success", true);

Map dataMap = new HashMap();
dataMap.put("id", 111);
dataMap.put("username", "岳元璞");

map.put("data", dataMap);

String jsonStr = JSONObject.fromObject(map).toString();
System.out.println(jsonStr);