先定义list集合,list转json字符串
//定义list集合
List list = new ArrayList();
list.add(new Student("张三", "123"));
list.add(new Student("李四", "456"));
//list转json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
json字符串转list
//json字符串转list
List list2 = new ArrayList();
String jsondata = "[{\"password\":\"123\",\"username\":\"张三\"},{\"password\":\"456\",\"username\":\"李四\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
for(int i = 0; i < jsonArray1.size(); i++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
Student stu2 = (Student)JSONObject.toBean(jsonObject2, Student.class);
list2.add(stu2);
}
System.out.println(list2);
全部代码
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//定义list集合
List list = new ArrayList();
list.add(new Student("张三", "123"));
list.add(new Student("李四", "456"));
//list转json字符串
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
//json字符串转list
List list2 = new ArrayList();
String jsondata = "[{\"password\":\"123\",\"username\":\"张三\"},{\"password\":\"456\",\"username\":\"李四\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
for(int i = 0; i < jsonArray1.size(); i++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
Student stu2 = (Student)JSONObject.toBean(jsonObject2, Student.class);
list2.add(stu2);
}
System.out.println(list2);
}
}
运行结果
Map与json字符串互转定义map集合,Map转json字符串
//定义map集合
Map map = new HashMap();
map.put("1", new Student("张三", "123"));
map.put("2", new Student("李四", "456"));
//Map转json字符串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);
定义字符串map集合,map集合字符串转为map
//定义字符串map集合
String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"张三\"},\"2\":{\"password\":\"456\",\"username\":\"李四\"}}";
//map集合字符串转为map
Map map2 = (Map)JSONObject.fromObject(jsondata);
Set set = map2.keySet();
//定义迭代器,迭代输出
Iterator ite = set.iterator();
while(ite.hasNext()) {
//取出一个字符串对象
String key = (String)ite.next();
//转为json格式
JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
//转为对象
Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);
System.out.println(key+" "+stu);
}
全部代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//定义map集合
Map map = new HashMap();
map.put("1", new Student("张三", "123"));
map.put("2", new Student("李四", "456"));
//Map转json字符串
JSONObject jsonMap = JSONObject.fromObject(map);
System.out.println(jsonMap);
//定义字符串map集合
String jsondata = "{\"1\":{\"password\":\"123\",\"username\":\"张三\"},\"2\":{\"password\":\"456\",\"username\":\"李四\"}}";
//map集合字符串转为map
Map map2 = (Map)JSONObject.fromObject(jsondata);
Set set = map2.keySet();
//定义迭代器,迭代输出
Iterator ite = set.iterator();
while(ite.hasNext()) {
//取出一个字符串对象
String key = (String)ite.next();
//转为json格式
JSONObject jsonObject = JSONObject.fromObject(map2.get(key));
//转为对象
Student stu = (Student)JSONObject.toBean(jsonObject, Student.class);
System.out.println(key+" "+stu);
}
}
}
运行结果
JSONArray与List互转定义list集合,List转型JSONArray
//定义list集合
List<Student> list = new ArrayList<Student>();
list.add(new Student("张三", "123"));
list.add(new Student("李四", "456"));
//List转型JSONArray
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
JSONArray转型List,JSONArray是用的上面的那个jsonArray变量
//JSONArray转型List
List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
Iterator<Student> ite = list2.iterator();
while(ite.hasNext()) {
Student stu = ite.next();
System.out.println(stu);
}
全部代码
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
public class Json {
public static void main(String[] args) {
//定义list集合
List<Student> list = new ArrayList<Student>();
list.add(new Student("张三", "123"));
list.add(new Student("李四", "456"));
//List转型JSONArray
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
//JSONArray转型List
List<Student> list2 = JSONArray.toList(jsonArray, new Student(), new JsonConfig());
Iterator<Student> ite = list2.iterator();
while(ite.hasNext()) {
Student stu = ite.next();
System.out.println(stu);
}
}
}
运行结果
JSONArray与数组互转定义数组,数组转JSONArray
//定义数组
boolean[] boolArray = {true, false, true};
//java数组转JSONArray
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray.toString());
JSONArray转java数组
//JSONArray转java数组
Object obj[] = jsonArray.toArray();
for(Object o : obj) {
System.out.print(o+"\t");
}
全部代码
import net.sf.json.JSONArray;
public class Json {
public static void main(String[] args) {
//定义数组
boolean[] boolArray = {true, false, true};
//java数组转JSONArray
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray.toString());
//JSONArray转java数组
Object obj[] = jsonArray.toArray();
for(Object o : obj) {
System.out.print(o+"\t");
}
}
}
运行结果
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |