servlet接收解析json数据

一、jar包准备

jar: fastJson / gson

二、前端代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<button id="btn">click</button>
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
 <script type="text/javascript">
 
 $("#btn").click(function(){
	 
	 var list = {"list":[{name:'sf',age:'fs'}]};
	 $.ajax({
		url:"log",
		
		type:'post',
		contentType: 'application/json',
		data: JSON.stringify(list),
		success:function(res){
			console.log(res);
			alert(res);
		}
	 });
 })
 </script>
</body>
</html>

三、工具类

package com.qst.until;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletInputStream;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtils {
	

	 public  static JSONArray getJson(InputStreamReader req) {
		 
			JSONObject json = new JSONObject();
			 JSONArray list = new JSONArray();
		        BufferedReader br;
		        String jsonStr = null;
		        String jsonStr2 = null;
		        try {
		            br = new BufferedReader(req);
		            String line = null;
		            StringBuilder sb = new StringBuilder();

		            while ((line = br.readLine()) != null) {
		                sb.append(line);
		            }
		            json= JSONObject.parseObject(sb.toString());
		            jsonStr = JSONObject.toJSONString(json);
		            list = json.getJSONArray("list");
		            jsonStr2 = JSONArray.toJSONString(list);
		        } catch (IOException e) {
		            e.printStackTrace();
		        }
		        return list;
		        
	 }

}

四、servlet代码

package com.qst.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.qst.until.JsonUtils;

@WebServlet("/log")
public class log extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		String name = null;

		InputStreamReader reader = new InputStreamReader((ServletInputStream) req.getInputStream());

		JSONArray list = JsonUtils.getJson(reader);

		System.out.println("list===============");
		System.out.println(list);
		System.out.println("逐个取出===============");
		for (int i = 0; i < list.size(); i++) {
			JSONObject object = (JSONObject) list.get(i);
			System.out.println("name" + object.getString("name") + "age" + object.getString("age"));
			name = object.getString("name");
		}

		resp.getWriter().write(name);
	}

}

servlet接收解析json数据

上一篇:java和json的兄弟情义


下一篇:微信小程序用URL也能打开了