java将string转为json |
您所在的位置:网站首页 › jsonarry遍历 › java将string转为json |
展开全部 string转json有三种方法: 第一种:string直接转json String json = "{\"2\":\"efg\",\"1\":\"abc\"}"; JSONObject json_test = JSONObject.fromObject(json); 将string的双引号转义即可,适用于字符串32313133353236313431303231363533e58685e5aeb931333365633964较短的 第二种:将string转为list后转为json Listlist = new ArrayList(); list.add("username"); list.add("age"); list.add("sex"); JSONArray array = new JSONArray(); array.add(list); 可以使用list的add函数将需要的字符串拼接即可,但是这个只能使用jsonarry 第三种:将string转为map后转为json Mapmap = new HashMap(); map.put("1", "abc"); map.put("2", "efg"); JSONArray array_test = new JSONArray(); array_test.add(map); JSONObject jsonObject = JSONObject.fromObject(map); 这里使用map就可以将字符串转化为JSONArray或者JSONObject都可以,但是这里的键不能使用int型 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |