package com.saic.grape.utils;
import java.util.Map;
import com.meidusa.fastjson.JSON;
import com.meidusa.fastjson.JSONObject;
import com.saic.grape.entity.KeyValue;
import com.saic.grape.entity.UserLogin;
/**
* Copyright (C), 2014-4-22,
* @version 1.0
* @date 2014-4-22 javaBean转化为字符串公共类
* @author SHENBO
*/
public class FastJsonUtil
{
/**
* 将javabean转化为序列化的json字符串
* @param keyvalue
* @return
*/
public static Object beanToJson(KeyValue keyvalue) {
String textJson = com.meidusa.fastjson.JSON.toJSONString(keyvalue);
Object objectJson = JSON.parse(textJson);
return objectJson;
}
/**
* 将string转化为序列化的json字符串
* @param keyvalue
* @return
*/
public static Object textToJson(String text) {
Object objectJson = JSON.parse(text);
return objectJson;
}
/**
* json字符串转化为map
* @param s
* @return
*/
public static Map stringToCollect(String s) {
Map m = (Map) JSONObject.parseObject(s);
return m;
}
/**
* 将map转化为string
* @param m
* @return
*/
public static String collectToString(Map m) {
String s = JSONObject.toJSONString(m);
return s;
}
/**
* json字符串转化为javabean
* @param s
* @return
*/
public static UserLogin jsonToJavaBean(String s) {
UserLogin userLogin = JSONObject.parseObject(s, UserLogin.class);
return userLogin;
}
public static void main(String[] args) {
String str = "{\"appCode\":\"Grape\",\"appVersion\":\"1.0\",\"deviceId\":\"236fa43ed352cc235\",\"sourceId\":\"1c1c1c\",\"userAccount\":\"13812345678\",\"userToken\":\"123456789\"}";
Map ma = FastJsonUtil.stringToCollect(str);
String ss = "{\"velModelId\":10000,\"velModelName\":\"奥迪\"}";
//LoginResultVelModel vd = FastJsonUtil.jsonToJavaBean(ss);
//System.out.println(vd.getVelModelName());
}
}
fastjson 第三方jar和java各种数据对象相互转化,布布扣,bubuko.com
fastjson 第三方jar和java各种数据对象相互转化
原文:http://blog.csdn.net/xiaogg3678/article/details/26587745