您的位置:首页 > Web前端 > JavaScript

fastjson来做Json转换例子

2015-07-04 14:57 651 查看

不多说废话了,比较简单,直接上代码吧,都是在本人电脑上跑过的。

说明下还有个Student的实体类没上传了,随便搞几个属性测试下就可以。

 

转换类:

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

public class HTTPHelper {

private static String jsonStr = "{\"mobilePhone\":\"13510398031\",\"userName\":\"李四\"}";
private static String jsonStr2 = "[{\"mobilePhone\":\"13510398031\",\"userName\":\"李四\"},{\"mobilePhone\":\"13824568145\",\"userName\":\"张三\"}]";

private static final JSON buildJSON(String jsonStr)
throws IOException {
return JSONSerializer.toJSON(jsonStr);
}

//字符串 转 JSONObject
public static final JSONObject buildJSONObject()
throws IOException {
return (JSONObject) buildJSON(jsonStr);
}

//字符串(数组) 转 JSONArray
public static final JSONArray buildJSONArray()
throws IOException {
return (JSONArray) buildJSON(jsonStr2);
}

// 字符串 转 JSONObject
public static final JSONObject buildJSONObjectFromParameter() throws IOException {
return JSONObject.fromObject(jsonStr);
}

// 字符串(数组) 转 JSONArray
public static final JSONArray buildJSONArrayFromParameter() throws IOException {
return JSONArray.fromObject(jsonStr2);
}

//Json字符串 转 实体对象
public static final <T> T getObject(Class<T> clazz) throws IOException {
return com.alibaba.fastjson.JSONObject.parseObject(jsonStr, clazz);
}

//Json字符串 转 数组对象
@SuppressWarnings("unchecked")
public static final <T> List<T> getList(Class<T> clazz) throws IOException {
List<T> list = com.alibaba.fastjson.JSONArray.parseArray(jsonStr2, clazz);
return list == null ? Collections.EMPTY_LIST : list;
}

// 对象 转 Json字符串
public static final String getJsonString(Object o) throws IOException {
return com.alibaba.fastjson.JSONObject.toJSONString(o);
}

// 对象 转 com.alibaba.fastjson.JSONObject对象
public static final com.alibaba.fastjson.JSONObject getJson(Object o) throws IOException {
return (com.alibaba.fastjson.JSONObject)com.alibaba.fastjson.JSONObject.toJSON(o);
}

//数组对象 转 Json字符串
public static final String getJsonArrayString(Object o) throws IOException {
return com.alibaba.fastjson.JSONArray.toJSONString(o);
}
}

 

 

测试代码:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.user.Student;
import com.utils.HTTPHelper;

public class HTTPHelperTest {

public static void main(String[] args) throws IOException {
// 字符串 转 JSONObject
JSONObject jsonObject = HTTPHelper.buildJSONObjectFromParameter();
String account = jsonObject.getString("mobilePhone");
String name = jsonObject.getString("userName");
System.out.println(account+"======================"+name);

// 字符串(数组) 转 JSONArray
JSONArray jsonArray = HTTPHelper.buildJSONArrayFromParameter();
System.out.println(jsonArray);
Iterator iterator = jsonArray.iterator();
while (iterator.hasNext()) {
JSONObject jsonObj = (JSONObject) iterator.next();
System.out.println(jsonObj.getString("userName"));
}

// 字符串(数组) 转 JSONArray
JSONArray jsonArr = HTTPHelper.buildJSONArray();
System.out.println(jsonArr);
Iterator iterator1 = jsonArr.iterator();
while (iterator1.hasNext()) {
JSONObject jsonObj = (JSONObject) iterator1.next();
System.out.println(jsonObj.getString("userName"));
}

//Json字符串 转 实体对象
Student s = HTTPHelper.getObject(Student.class);
System.out.println(s.getUserName());

//Json字符串 转 数组对象
List<Student> list = HTTPHelper.getList(Student.class);
System.out.println(list.size());
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getUserName());
}

// 对象 转 Json字符串
Student stu = new Student();
stu.setAge(45);
stu.setBirthDay(new Date());
List<String> hobbiy = new ArrayList<String>();
hobbiy.add("唱歌");hobbiy.add("跳舞");hobbiy.add("爬山");
stu.setHobbiy(hobbiy);
stu.setUserName("张三");
System.out.println(HTTPHelper.getJsonString(stu));

// 对象 转 com.alibaba.fastjson.JSONObject对象
com.alibaba.fastjson.JSONObject jsonObj = HTTPHelper.getJson(stu);
System.out.println(jsonObj.getString("userName"));

//数组对象 转 Json字符串
List<Student> listStudent = new ArrayList<Student>();
listStudent.add(stu);
System.out.println(HTTPHelper.getJsonArrayString(listStudent));
}
}

 

使用的主要jar包:

  fastjson-1.2.5.jar

  json-lib-2.4-jdk15.jar

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: