String jsonString=JSON.toJSONString(map); System.out.println("json字符串是:"+jsonString); } 运行结果: 运行结果: json字符串是:{"name":"cool_summer_moon","age":24} 5、Map 转 Json @Testpublicvoidtest(){ Map<String,Object> map =newHashMap<>(); map.put("age", 24); map.put("nam...
JSONObject jsonObject=JSONObject.parseObject(str);//json对象转MapMap<String,Object> map = (Map<String,Object>)jsonObject; Object object= map.get("age");5、Map转String Map<String,Object> map =newHashMap<>(); map.put("age", 24); map.put("name", "cool_summer_moon"); String jsonSt...
String jsonString = jsonObject.toJSONString(); System.out.println("json字符串是:" + jsonString); } 1 2 3 4 5 6 7 8 运行结果: json字符串是:{"name":"cool_summer_moon","age":"24"} 1 2 3、String 转 Map @Test public void test(){ String str = "{\"age\":\"24\",\"name\...
1- String 转 HashMap<> - 对应的map结构都可以 比如以下可以转换成HashMap 也可以是其他map类型 Map<String,Object> map = JSONObject.parseObject(json,new TypeReference<HashMap<String,Object>>(){}); 2- String 转 List 或者其他集合 List<Map<String,Object>> listMap = JSONObject.parseObject(json,n...
一、json与对象互转 先新建个Student对象 public class Student { //姓名 private String name; //年龄 private String age; //性别 private String sex; //地址 private String address; //生日 private Date birthday; } 1. 2. 3. 4. 5.
5.map格式 转JSONObject publicstaticvoidmyJson(Stringstr){Map<String,Object>map=newHashMap<String,Object>();map.put("name","张三");map.put("age","18");JSONObjectjsonObj=newJSONObject(map);// 测试结果System.out.println(jsonObj.toString());} ...
publicstaticvoidmap2Json(){Map<String,Object>map=newHashMap<>();map.put("code",401);map.put("data","fail");map.put("message","账号已在别处登录,请重新登陆");System.out.println("map :"+"\n"+map);//map转换为JSONJSONObject jsonObject=newJSONObject(map);System.out.println("map to...
转Map//map转字符串String jsonString=JSON.toJSONString(map);5.Map 转 Json对象//map转json对象Map<String,Object>map=newHashMap<>();map.put("age",24);map.put("name","cool_summer_moon");JSONObject json=newJSONObject(map);//json对象转MapMap<String,Object>map=(Map<String,Object>)json...
生成JSON代码片段 [java] Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonMap.put(“a”,1); jsonMap.put(“b”,“”); jsonMap.put(“c”,null); jsonMap.put(“d”,“wuzhuti.cn”); String str = JSONObject.toJSONString(jsonMap); ...