使用json-lib将java对象转换成json字符串
学会了在j2ee中使用ajax后,有时候从服务器返回客户端的数据是对象,自己手动拼凑json字符串时容易出错,通过谷歌知道了json-lib这个类库,方便的支持java对象到json字符串的转化。
看source-forge的json-lib介绍说,需要
jakarta commons-lang 2.4
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1
ezmorph 1.0.6
这些jar包的支持,下载下来试了一把,挺好用的。代码如下(分为三种情况:1 对象 2 List 3 对象和List)
1 对象:
view plaincopy to clipboardprint?
package com.json.action.main;
import java.text.SimpleDateFormat;
import net.sf.json.JSONObject;
import com.json.model.UserModel;
public class UserObjectJsonMain {
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
UserModel ua=new UserModel();
ua.setId(11);
ua.setTitle("JSON传输 ");
ua.setContent("java对象转json文件格式! ");
ua.setCurrent(sdf.format(new java.util.Date()));
JSONObject jsonObject = JSONObject.fromObject(ua);
System.out.println("java Object to json : "+ jsonObject);
}
}
package com.json.action.main;
import java.text.SimpleDateFormat;
import net.sf.json.JSONObject;
import com.json.model.UserModel;
public class UserObjectJsonMain {
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
UserModel ua=new UserModel();
ua.setId(11);
ua.setTitle("JSON传输 ");
ua.setContent("java对象转json文件格式! ");
ua.setCurrent(sdf.format(new java.util.Date()));
JSONObject jsonObject = JSONObject.fromObject(ua);
System.out.println("java Object to json : "+ jsonObject);
}
}
2 List
view plaincopy to clipboardprint?
package com.json.action.main;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import com.json.model.UserModel;
public class UserListJsonMain {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<UserModel> list = new ArrayList<UserModel>();
for (int i = 1; i < 10; i++) {
UserModel user = new UserModel();
user.setId(i);
user.setTitle("JSON传输 " + i);
user.setContent("java对象转json文件格式! " + i);
user.setCurrent(sdf.format(new java.util.Date()));
list.add(user);
}
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
}
}
package com.json.action.main;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import com.json.model.UserModel;
public class UserListJsonMain {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<UserModel> list = new ArrayList<UserModel>();
for (int i = 1; i < 10; i++) {
UserModel user = new UserModel();
user.setId(i);
user.setTitle("JSON传输 " + i);
user.setContent("java对象转json文件格式! " + i);
user.setCurrent(sdf.format(new java.util.Date()));
list.add(user);
}
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
}
}
3 对象和List
view plaincopy to clipboardprint?
package com.json.action.main;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import com.json.model.UserModel;
public class UserListObjectJsonMain {
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
UserModel ua=new UserModel();
ua.setId(11);
ua.setTitle("JSON传输 ");
ua.setContent("java对象转json文件格式! ");
ua.setCurrent(sdf.format(new java.util.Date()));
List<UserModel> list = new ArrayList<UserModel>();
for (int i = 1; i < 10; i++) {
UserModel user = new UserModel();
user.setId(i);
user.setTitle("JSON传输 " + i);
user.setContent("java对象转json文件格式! " + i);
user.setCurrent(sdf.format(new java.util.Date()));
list.add(user);
}
//当既有对象又有数组的时候
Map<String,Object> map = new HashMap<String,Object>();
map.put("ua",ua);
map.put("lis",list);
JSONObject jsonObjectFromMap = JSONObject.fromObject(map);
System.out.println(jsonObjectFromMap);
}
}