展开全部

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型

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:12 天前 )
960b763e 3 个月前
8c391e04 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐