目录
1、使用场景
由于在于前端页面参数交换的时候可能传递多行记录值或者对象Array,在Java后端接收后需要处理相关数据,此时需要针对传入的数据进行转为的JSON对象或者JSONArray对象。于是有了使用阿里巴巴之中的相关JSON包 fastjson进行转换。
2、实战代码
1、//将map转换成jsonObject
JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(itemMap));
将Map类型的itemInfo转换成json,再经JSONObject转换实现。
2、//将jsonObj转换成Map
Map<String, Object> itemMap = JSONObject.toJavaObject(itemJSONObj, Map.class);
//JOSN.parseObjet()方法同样可以转换
3、//将List转换成JSONArray
JSONArray ja = JSONArray.parseArray(JSON.toJSONString(itemList));
4、json转成对象
List<ChannelItem> channelItemList = JSON.parseArray(itemJson,ChannelItem.class);
ProducePaperBaseInfo producePaperBaseInfo = JSON.parseObject(
paperBaseInfoJsonStr,
new TypeReference<ProducePaperBaseInfo>() {
});
5、将String转换成ArrayList 对象
ArrayList<HashMap<String,String>> paperAddOrDeleteQuestionList = JSON.parseObject(
intelligentAddQuestionListStr,
new TypeReference<ArrayList<HashMap<String,String>>>() {
});