欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA开发 JSON类型转换 

程序员文章站 2024-03-22 15:43:40
...
JSON 类型转换应用:

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class JSONUtil {
public static String objToJSONStr(Object object) {
return JSON.toJSONString(object);
}

public static <T> T jsonStrToObj(String text, Class<T> clazz) throws BusinessServiceException {
try {
return JSON.parseObject(text, clazz);
} catch (Exception e) {
throw new BusinessServiceException("解析json字符串出错," + e.getMessage(), e);
}

}

public static JSONObject jsonStrToObj(String text) throws BusinessServiceException {
try {
return JSON.parseObject(text);
} catch (Exception e) {
throw new BusinessServiceException("解析json字符串出错," + e.getMessage(), e);
}
}

public static <T> List<T> parseArray(String text, Class<T> clazz) throws BusinessServiceException {
try {
return JSON.parseArray(text, clazz);
} catch (Exception e) {
throw new BusinessServiceException("解析json字符串出错," + e.getMessage(), e);
}
}
}

//json应用需要jar支持:fastjson-1.1.26.jar
相关标签: JSON类型转换