如何将json转化为字符串,java将string转为json
目录
将数据字符串转换成地图/列表字符串,json,Map互相转换(各种转换都有)前提:使用冲突包为使用程序如下
将json字符串转换成map/list
导入Java。util。ArrayList导入Java。util。hashmap导入Java。util。迭代器;导入Java。util。列表;导入Java。util。地图;导入org。代号豪斯。斯波吉。JSON。JSON数组;导入org。代号豪斯。斯波雷特。JSON。JSON对象;公共类JSON str 2ma民意调查者{/* * *以{开头的jsonStr转地图,地图里面有列表,列表里面还装有map */@抑制警告(“原始类型”)公共静态MapString,Object JSON str 2 map(String JSON str)抛出异常{ MapString,Object map=new HashMapString,Object();如果(!JSON海峡。isempty()){ JSON object JSON=new JSON object(JSON str);迭代器I=JSON。keys();while(I . has next()){ String key=(String)I . next();字符串值=JSON。getstring(key);如果(值。( { )==0){ map的索引。放(键。trim()、JSON str 2 map(value));} else if(值。的索引([)==0){ map。放(键。trim()、JSON str 2 list(value));} else { map.put(key.trim()、value。trim());} } }回图;} /** * 以[开头的线转列表,列表里装有map */public static ListMapString,Object JSON str 2 list(String JSON str)抛出异常{ ListMapString,Object list=new ArrayListMapString,Object();JSON数组ja=新JSON数组(JSON str);for(int j=0;杰杰。长度();j){字符串JM=ja。getstring(j);如果(JM。( { )==0){ MapString,Object m=JSON str 2 map的索引(JM);列表。添加(m);} }返回列表;} /** * jsonStr和地图之间的转换,jsonStr和目录之间的转换* * @ param args * @ throws Exception */public static void main(String[]args)抛出异常{ //1.jsonStr和地图之间的转换MapString,Object map=new HashMapString,Object();map.put(key1 , val 1 );map.put(key2 , val 2 );ListMapString,对象列表=新
ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); Map<String, Object> map2 = new HashMap<String, Object>(); map1.put("key11", "val11"); map1.put("key21", "val21"); map2.put("key111", "val111"); map2.put("key211", "val211"); list.add(map1); list.add(map2); map.put("keyList", list); System.out.println("原始的map:" + map); String map2JsonStr = map.toString(); System.out.println("map2JsonStr:" + map2JsonStr); Map<String, Object> jsonStr2Map = jsonStr2Map(map2JsonStr); System.out.println("jsonStr2Map:" + jsonStr2Map); // 2.jsonStr和List之间的转换 List<Object> list_ = new ArrayList<Object>(); list_.add(map); String list_Str = list_.toString(); System.out.println("list_Str:" + list_Str); List<Map<String, Object>> listM = jsonStr2List(list_Str); System.out.println("listM:" + listM); }}输出结果:
原始的map:{key2=val2, key1=val1, keyList=[{key11=val11, key21=val21}, {key211=val211, key111=val111}]}map2JsonStr:{key2=val2, key1=val1, keyList=[{key11=val11, key21=val21}, {key211=val211, key111=val111}]}jsonStr2Map:{key2=val2, key1=val1, keyList=[{key11=val11, key21=val21}, {key211=val211, key111=val111}]}list_Str:[{key2=val2, key1=val1, keyList=[{key11=val11, key21=val21}, {key211=val211, key111=val111}]}]listM:[{key2=val2, key1=val1, keyList=[{key11=val11, key21=val21}, {key211=val211, key111=val111}]}]
字符串,json,Map互相转换(各种转换都有)
前提:使用jar包为fastjson
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;
程序如下
Map map = new HashMap(); map.put("AAA", "1"); map.put("BBB", "2"); map.put("CCC", "3"); System.out.println("map=>"+map); //1.map转string String jsonString = JSON.toJSONString(map); System.out.println("mapToString=>"+jsonString); //2.map转jsonObject JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(map)); System.out.println("mapToJSON=>"+JSONObj); //3.String转jsonObject String jsonString2 = "{"AAA":"1","CCC":"3","BBB":"2"}"; JSONObject parseObject = JSONObject.parseObject(jsonString2); System.out.println("StringToJson=>"+parseObject); //4.String转map Map stringToMap = JSONObject.parseObject(jsonString2); System.out.println("StringToMap=>"+stringToMap); String str = "{"age":"24","name":"summer"}"; JSONObject jsonObject = JSONObject.parseObject(str); //5.jsonObject转map Map jsonToMap = JSONObject.parseObject(jsonObject.toJSONString()); System.out.println("jsonToMap=>"+jsonToMap); //6.jsonObject转String String jsonString3 = jsonObject.toJSONString(); System.out.println("jsonToString=>"+jsonString3);
结果如下
map=>{AAA=1, CCC=3, BBB=2}mapToString=>{"AAA":"1","CCC":"3","BBB":"2"}mapToJSON=>{"AAA":"1","CCC":"3","BBB":"2"}StringToJson=>{"AAA":"1","CCC":"3","BBB":"2"}StringToMap=>{"AAA":"1","CCC":"3","BBB":"2"}jsonToMap=>{"name":"summer","age":"24"}jsonToString=>{"name":"summer","age":"24"}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。