中易网

如何将string转换成json

答案:1  悬赏:60  
解决时间 2021-01-15 00:59
  • 提问者网友:火车头
  • 2021-01-14 06:40
如何将string转换成json
最佳答案
  • 二级知识专家网友:醉吻情书
  • 2021-01-14 07:21
string转json有三种方法
第一种:string直接转json
String json = "{"2":"efg","1":"abc"}"; JSONObject json_test =
JSONObject.fromObject(json); 将string的双引号转义即可,适用于字符串较短的
第二种:将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型
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息