import java.util.List;
import com.alibaba.fastjson.JSON;
import com.yz.javabean.Student;
import com.yz.utils.HttpUtils;
public class Demo11FastJson {
public static void main(String[] args) {
String baseUrl1 = "http://10.0.158.239:8080/Day22_Server/JsonServlet?action=student";
String baseUrl2 = "http://10.0.158.239:8080/Day22_Server/JsonServlet?action=students";
String json1 = HttpUtils.getJsonFromNetWork(baseUrl1);// {}
String json2 = HttpUtils.getJsonFromNetWork(baseUrl2);// []
//阿里巴巴FAST解析:解析对象
Student stu = JSON.parseObject(json1, Student.class);
System.out.println(stu);
//阿里巴巴Fast-json:解析一个集合
List<Student> list = JSON.parseArray(json2, Student.class);
}
}
import com.alibaba.fastjson.JSON;
import com.yz.javabean.Student;
import com.yz.utils.HttpUtils;
public class Demo11FastJson {
public static void main(String[] args) {
String baseUrl1 = "http://10.0.158.239:8080/Day22_Server/JsonServlet?action=student";
String baseUrl2 = "http://10.0.158.239:8080/Day22_Server/JsonServlet?action=students";
String json1 = HttpUtils.getJsonFromNetWork(baseUrl1);// {}
String json2 = HttpUtils.getJsonFromNetWork(baseUrl2);// []
//阿里巴巴FAST解析:解析对象
Student stu = JSON.parseObject(json1, Student.class);
System.out.println(stu);
//阿里巴巴Fast-json:解析一个集合
List<Student> list = JSON.parseArray(json2, Student.class);
}
}
本文展示如何利用FastJson库从网络获取并解析JSON数据,包括单个对象和对象列表的解析过程。
610

被折叠的 条评论
为什么被折叠?



