Json変換
7523 ワード
次に、テキスト回転json、配列回転json、listリスト回転json、setリスト回転json、mapテーブル回転json、javaBean装換jsonについて説明します.
package Web;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.map.HashedMap;
import Bean.StudentBean;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
/**
* Servlet implementation class JsonServlet
*/
@WebServlet("/JsonServlet")
public class JsonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JsonServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/*response.getWriter().append("Served at: ").append(request.getContextPath());*/
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
// JSON
out.println("{\"id\":1,\"name\": ,\"pwd\":1234aa}");
// json
String []num = {"123"," ","ahjdgf"};
JSONArray StrNum = JSONArray.fromObject(num);
out.println("
");
out.println(StrNum);
//list json
List strJson = new ArrayList();
strJson.add(" ");
strJson.add("1");
strJson.add("fdg");
JSONArray Num = JSONArray.fromObject(strJson);
out.println("
");
out.println(Num);
//set json
Set