利用fastJson、Gson做json以及String的互相转换
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
fastJson
1、String转换为JSONObject
String param = "{\"age\":\"24\",\"name\":\"cool_summer_moon\"}";
JSONObject jsonObject = JSONObject.parseObject(param);
Object object = jsonObject.get("name");
2、JSONObject转换为String
String jsonString = jsonObject.toJSONString();
3、JsonString转换为Object
String jsonString = jsonObject.toJSONString();
Info info = JSON.parseObject(jsonString, Info.class);
4、String 转 Map
String str = "{\"age\":\"24\",\"name\":\"cool_summer_moon\"}";
JSONObject jsonObject = JSONObject.parseObject(str);
//json对象转Map
Map<String,Object> map = (Map<String,Object>)jsonObject;
Object object = map.get("age");
5、Map转String
Map<String,Object> map = new HashMap<>();
map.put("age", 24);
map.put("name", "cool_summer_moon");
String jsonString = JSON.toJSONString(map);
System.out.println("json字符串是:"+jsonString);
运行结果:
json字符串是:{"name":"cool_summer_moon","age":24}
6、Map转换为Json
Map<String,Object> map = new HashMap<>();
map.put("age", 24);
map.put("name", "cool_summer_moon");
JSONObject json = new JSONObject(map);
System.out.println("Json对象是:" + json);
运行结果:
Json对象是:{"name":"cool_summer_moon","age":24}
7、获取JSONObject中的JSONObject
String str = "{\"age\":\"24\",\"info\":{\"name\":\"cool_summer_moon\",\"time\":\"201902131331\"}}";
JSONObject jsonObject = JSONObject.parseObject(str);
JSONObject info = jsonObject.getJSONObject("info");
info.get("name").toString();
System.out.println("data==>"+info.toString());
System.out.println("name==>"+ info.get("name").toString());
返回值:
data==>{"name":"cool_summer_moon","time":"201902131331"}
name==>cool_summer_moon
gson:
1、string转换为object
Gson gson = new Gson();
String deployJson = pDeploy.getDeploymentJson();
Deployment deployment = gson.fromJson(deployJson,Deployment.class);
2、object转换为String
Deployment deployment = new Deployment();
Gson gson = new Gson();
pDeployment.setDeploymentJson(gson.toJson(deployment));
3、map转换为json
Gson gson = new Gson();
Map map = Maps.newHashMap();
map.put("colour", "red");
map.put("weight", "10kg");
gson.toJson(map);
4、json转换为map
Gson gson = new Gson();
Map map = Maps.newHashMap();
map.put("colour", "red");
map.put("weight", "10kg");
String mapJson = gson.toJson(map);
map = gson.fromJson(mapJson,Map.class);
jackson
ObjectMapper om = new ObjectMapper();
List<Map<String,Object>> list = om.readValue(temp, new TypeReference<List<Map<String, Object>>>() {});
/**
* description 生成参数
* @param orderBean
* @return
*/
public JSONObject getOrderBody(OrderBean orderBean){
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String timestamp = df.format(LocalDateTime.now()) + "";
TreeMap<String, Object> params = new TreeMap<String, Object>();
params.put("appId", orderBean.getAppId());
params.put("timestamp", timestamp);
params.put("version", orderBean.getVersion());
params.put("serviceCode", orderBean.getServiceCode());
params.put("phoneId", orderBean.getPhoneId());
params.put("orderType", orderBean.getOrderType());
params.put("activityId", orderBean.getActivityId());
params.put("skuId", orderBean.getSkuId());
orderBean.setCustomerOrderId(timestamp+orderBean.getPhoneId());
params.put("customerOrderId", orderBean.getCustomerOrderId());
params.put("effectType", orderBean.getEffectType());
params.put("orderCount", orderBean.getOrderCount());
params.put("notifyUrl", orderBean.getNotifyUrl());
JSONObject attach = new JSONObject();
attach.put("destinationIp", orderBean.getDestinationIP());
attach.put("userIp", orderBean.getPrivateIP());
attach.put("token", orderBean.getQosToken());
params.put("attach", attach.toString());
String sign = "";
try {
sign = HmacUtil.signTopRequest(params, APP_KEY);
} catch (IOException e) {
e.printStackTrace();
}
JSONObject header = new JSONObject();
header.put("appId", orderBean.getAppId());
header.put("serviceCode", orderBean.getServiceCode());
header.put("timestamp", timestamp);
header.put("version", orderBean.getVersion());
header.put("sign", sign);
JSONObject parameter = new JSONObject();
parameter.put("customerOrderId", orderBean.getCustomerOrderId());
parameter.put("phoneId", orderBean.getPhoneId());
parameter.put("notifyUrl", orderBean.getNotifyUrl());
parameter.put("orderType", orderBean.getOrderType());
parameter.put("skuId", orderBean.getSkuId());
parameter.put("activityId", orderBean.getActivityId());
parameter.put("effectType", orderBean.getEffectType());
parameter.put("orderCount", orderBean.getOrderCount());
parameter.put("attach", attach.toString());
JSONObject reqParam = new JSONObject();
reqParam.put("header", header.toString());
reqParam.put("params", parameter.toString());
return reqParam;
}
JSONObject reqParam = accessUtil.getOrderBody(orderBean);
// 返回值
String resp = RequestUtil.httpReqForJsonWithPostMethod(AccessConstants.endpoint, reqParam.toString());
JSONObject respObject = JSONObject.fromObject(resp);
if(respObject.containsKey("resCode") && respObject.containsKey("resDesc")){
map.put("resCode",respObject.getString("resCode"));
map.put("resDesc",respObject.getString("resDesc"));
}else {
map.putAll(ResultStatesEnum.ORDER_FAILURE.toMap());
}
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献1条内容
所有评论(0)