JAVA开发

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类型转换)