1、string转json

有三种方法

第一种:string直接转json

String json = "{\"2\":\"efg\",\"1\":\"abc\"}";   JSONObject json_test = JSONObject.fromObject(json); 将string的双引号转义即可,适用于字符串较短的

第二种:将string转为list后转为json

List list = new ArrayList();  list.add("username");  list.add("age");  list.add("sex");  JSONArray array = new JSONArray();  array.add(list);

可以使用list的add函数将需要的字符串拼接即可,但是这个只能使用jsonarry

第三种:

将string转为map后转为json
Map map = 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型

1、json转string

先构造json:JSONObject string_to_json = JSONObject.fromObject("{\"data\": {\"pages\": [ {\"comment\": \"just for test\"},{\"comment\": \"just for test\"}],\"total_count\": 2 },\"errcode\": 0}");

对于JSONObject而言就可以直接使用

JSONObject json_to_data = string_to_json.getJSONObject("data");即可

对于JSONArray而言就可以使用这两种

第一种:

JSONArray json_to_strings = json_to_data.getJSONArray("pages");//先将JSONObject里包含的JSONArray取出
for (Object object : json_to_strings) {//循环读取即可
JSONObject json_to_string = JSONObject.fromObject(object);
json_to_string.get("pages");
}

第二种

:JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");//先将JSONObject里包含的JSONArray取出
for (int i = 0; i < json_to_strings_test.size(); i++) {//循环读取即可
JSONObject json_to_string1 = json_to_strings_test.getJSONObject(i);
}

上面全部程序的测试如图:

java string 转化成json java中string转json_java string 转化成json

下面贴出代码:

//string构筑json
String json = "{\"2\":\"efg\",\"1\":\"abc\"}";
JSONObject json_test = JSONObject.fromObject(json);
System.out.print("json_test:"+json_test);
System.out.print("\n");
//使用list构筑json(list只能使用JSONArray)
List list = new ArrayList();
list.add("username");
list.add("age");
list.add("sex");
JSONArray array = new JSONArray();
array.add(list);
System.out.print("array:"+array);
System.out.print("\n");
//初始化HashMap集合并添加数组(json必须键不能是int类型)
Map map = new HashMap();
map.put("1", "abc");
map.put("2", "efg");
JSONArray array_test = new JSONArray();
array_test.add(map);
System.out.print("array_test:"+array_test);
System.out.print("\n");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.print("jsonObject:"+jsonObject);
System.out.print("\n");
//解析json
JSONObject string_to_json = JSONObject.fromObject("{\"data\": {\"pages\": [ {\"comment\": \"just for test1\"},{\"comment\": \"just for test2\"}],\"total_count\": 2 },\"errcode\": 0}");
JSONObject json_to_data = string_to_json.getJSONObject("data");
JSONArray json_to_strings = json_to_data.getJSONArray("pages");
for (Object object : json_to_strings) {
JSONObject json_to_string = JSONObject.fromObject(object);
json_to_string.get("pages");
System.out.print("json_to_string.get(\"pages\"):"+json_to_string.get("comment"));
System.out.print("\n");
}
JSONObject json_to_data1 = string_to_json.getJSONObject("data");
JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");
for (int i = 0; i < json_to_strings_test.size(); i++) {
JSONObject json_to_string1 = json_to_strings_test.getJSONObject(i);
System.out.print("json_to_string1.get(\"pages\"):"+json_to_string1.get("comment"));
System.out.print("\n");
}

有新的好的方法希望能够讨论