import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by fuguowen on 2018/11/21 19:33
*/
public class test01 {
public static void main(String[] args) {
// 把字符串转化为map
String str = "{\"studentName\":\"lily\",\"student\":12}";
Map<String,Object> map =JSON.parseObject(str);
System.out.println(map);
// 把字符串转化为list
String str2 = "[{\"name\":\"lily\",\"age\":12},{\"name\":\"lucy\",\"age\":15}]";
List list=JSON.parseArray(str2);
System.out.println(list);
String str3= "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
Map<String,Object> map2= JSON.parseObject(str3);
System.out.println(map2);
Integer teacherAge = (Integer) map2.get("teacherAge");
System.out.println(teacherAge);
Map<String,Object> courseMap = (Map<String,Object>)map2.get("course");
System.out.println(courseMap);
List<Map<String,Object>> students = (List<Map<String,Object>>)map2.get("students");
System.out.println(students);
for (Map<String, Object> student : students) {
Set<Map.Entry<String, Object>> entries = student.entrySet();
for (Map.Entry<String, Object> entry : entries) {
System.out.println(entry.getKey()+"===="+entry.getValue());
}
}
Student student = JSON.parseObject(str, new TypeReference<Student>() {});
System.out.println(student);
}
}
Fastjson 解析字符串
猜你喜欢
转载自blog.csdn.net/u011243684/article/details/84330087
今日推荐
周排行