json,string和object的转换

现在主流的平台的接口数据都已经是json格式的了。简单介绍集中转换的方法,都是用第三方jar包。

1. jackson.

官方网站:http://jackson.codehaus.org/    我现在用的 jackson-all-1.9.1.jar  http://pan.baidu.com/s/1gd87aGr 用法

private static ObjectMapper objectMapper = new ObjectMapper();
    static{
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        objectMapper.setDeserializationConfig(objectMapper.getDeserializationConfig().without(                  
                DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));
    }
    /**
     * 使用对象进行json反序列化。
     * @param json json串
     * @param pojoClass 类类型 json的String类型转换为类
     */
    public static Object decodeJson(String json, Class pojoClass) throws Exception{     
        try{
            return objectMapper.readValue(json, pojoClass);
        }catch(Exception e){
            throw e;
        }
    }


    /**
     * 将对象序列化。
     * @param o 实体对象
     * @return 序列化后json object转String,可以转换map等类型
     */
    public static String encodeJson(Object o) throws Exception{
        try{
            return objectMapper.writeValueAsString(o);
        }catch(Exception e){
            throw e;
        }
    }

2. FastJson 

阿里巴巴FastJson也是一个Json工具包,速度最快的包,据说测试超越其他Java Json parser,超越JackJson,支持java Bean、集合、Map、日期、Enum,支持泛型支持自省;能够直接运行在java环境,支持Android,而且开源的。

API入口类是com.alibaba.fastjson.JSON 常用的操作

public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
public static final  T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean 
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
public static final  List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。

我的jar包:http://pan.baidu.com/s/1dDxfK21 加一个request的解析,解析完直接转换为java类,

private String getRequestBody(HttpServletRequest request) throws IOException {
String postBody = new String();
BufferedReader bReader = request.getReader();
        String line = null;
        while((line = bReader.readLine()) != null){
            postBody += line;
        }
return postBody;
}
pay ucReq = JSON.parseObject(postBody, pay.class);


3.Gson

google的Gson包,我的jar包。

	    JsonParser json = new JsonParser();
            JsonObject obj = (JsonObject) json.parse(content);
            String orderId = obj.get("orderId").toString();
            String money = obj.get("money").toString();

4.json。

需要两个包json-smart.jarjson-lib-2.4-jdk15.jar

JSONParser jsonParser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
   JSONObject obj;
   try {
       obj = (JSONObject) jsonParser.parse(jsonStr);
   } catch (ParseException e) {
       throw new QException(QException.CODE_JSON_ERROR, jsonStr);
   } catch (Exception e1) {
       throw new QException(QException.CODE_JSON_ERROR, jsonStr);
   }
   String errorCode = (String) obj.get("error_code");



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值