JSON的格式分爲兩種,第一種相似於JavaScript中的對象(這裏須要注意的是JS中的對象與java中的對象概念是不同的,js能夠在對象中定義屬性,方法等,相似於java中的類,可是java中的對象其實是類實例化的結果,是一個類的實例);第二種相似對象數組格式。java
下面建立js對象:兩個對象obj1和obj2,一個包含兩個對象的數組;ajax
var obj1 = { name: "小明", age: 25 } var obj2 = { name = "小王", age = 19 } var arr = [obj1,obj2];
假如咱們只須要發送某個對象的name和age值到後臺,咱們只須要發送obj1或者obj2就行。下面是JS代碼json
$.ajax({ async: true, url: "parseJson.do", data: JSON.stringify(obj1), processData: false, success: function(data){ //some code } })
是否是很簡單,下面建立一個servlet來接收前臺發過來的數據數組
//省略import各類包
async
@WebServlet("/parseJson.do") public class ParseJsonServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先建立字符流來讀取json BufferedReader reader = new BufferedReader(req.getReader()); String json = reader.readLine(); //這裏用fastjson來解析 JSONObject jo = JSON.parseObject(json); //獲取name和age對應的值 String name = jo.getString("name"); int age = jo.getInteger("age"); System.out.println("name: "+name+";"+"age: "+age); } }
運行結果:ide
因而後臺順利得到前臺傳來的值;this
首先構建數組格式的json:就是建立對象數組,前臺代碼url
$.ajax({ async: true, url: "parseJson.do",//URL沒有變意味着我用同一個servlet哦,演示本例的時候吧以前的都註釋掉了 data: JSON.stringify(arr), processData: false, success: function(data){ //some code } })
後臺仍是用servletspa
//省略import各類包 @WebServlet("/parseJson.do") public class ParseJsonServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先建立字符流來讀取json BufferedReader reader = new BufferedReader(req.getReader()); String json = reader.readLine(); //這裏用fastjson來解析數組,我並不知道解析的數據是哪一個對象,因此用了object的集合 List<Object> list = JSONArray.parseArray(json); //輸出 System.out.println(list.get(0)); System.out.println(list.get(1)); } }
輸出結果:3d
這不是輸出的兩行「JSON」嗎?我如何獲取name和age的值呢?
方法1:建立包含name和age成員變量的實體類,利用get方法不就能獲取值了!
public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
接着咱們修改servlet:
//省略import各類包 @WebServlet("/parseJson.do") public class ParseJsonServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先建立字符流來讀取json BufferedReader reader = new BufferedReader(req.getReader()); String json = reader.readLine(); //這裏用fastjson來解析數組,我並不知道解析的數據是哪一個對象,因此用了object的集合 //List<Object> list = JSONArray.parseArray(json); //利用JSONArray.parseArray()方法的重載 List<Person> list = JSONArray.parseArray(json,Person.class); System.out.print("obj1的姓名" + list.get(0).getName()); System.out.print("obj2的年齡" + list.get(1).getAge()); } }
輸出結果:
這樣順利的獲取name和age的值
方法2:我不想建立實體類,我對獲得的相似於"JSON"的東西繼續進行解析
仍是修改servlet:
//省略import各類包 @WebServlet("/parseJson.do") public class ParseJsonServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先建立字符流來讀取json BufferedReader reader = new BufferedReader(req.getReader()); String json = reader.readLine(); //這裏用fastjson來解析數組,我並不知道解析的數據是哪一個對象,因此用了object的集合 //List<Object> list = JSONArray.parseArray(json); //解析第一個'JSON' JSONObject jo1 = JSON.parseObject(String.valueOf(list.get(0))); //獲取obj1的name String name1 = jo1.getString("name"); //獲取obj2的age JSONObject jo2 = JSON.parseObject(String.valueOf(list.get(1))); int age2 = jo2.getInteger("age"); System.out.println("obj1的name: "+name1+"obj2的age"+age2); } }
運行的結果:
一樣成功得到前臺數據