2018.7.27 JSON and Java to convert each other

Source: Internet
Author: User
Tags string to json

json.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><% @taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> <! DOCTYPE html>
Jsonservlet
Package Servlet;import Java.io.ioexception;import Java.util.arraylist;import java.util.hashmap;import Java.util.list;import Java.util.map;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 domain. Person;import net.sf.json.jsonarray;import net.sf.json.JSONObject; @WebServlet ("/jsonservlet") public class    Jsonservlet extends HttpServlet {private static final long serialversionuid = 1L; protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep    tion {doPost (request, response); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, I        Oexception {String method = Request.getparameter ("method"); if (Method.equals ("Entityjson) {//Set encoding format otherwise garbled response.setcontenttype ("Text/html;charset=utf-8") will be displayed;            Person p = new person (1001, "Zhang San", "Male", "Guangxi"); 1.            Java entity to JSON object Jsonobject jsonobj = Jsonobject.fromobject (p);            Send data string Form Response.getwriter (). println (Jsonobj);                    System.out.println (Jsonobj); }else if (method.equals ("Jsonlist")) {//Set encoding format otherwise it will display garbled response.setcontenttype ("Text/html;charset=ut            F-8 ");            list<person> pList = new arraylist<person> ();            person P1 = new person (1001, "Zhang San", "Male", "Guangxi");            person P2 = new Person (1002, "John Doe", "female", "Guangdong");            Person P3 = new person (1003, "Harry", "Male", "Sichuan");            Plist.add (p1);            Plist.add (p2);            Plist.add (p3);            Jsonarray jsonobject = Jsonarray.fromobject (pList);            System.out.println ("Set length is" +jsonobject.size ()); Response.getwriter (). println (JsonobjeCT); }else if (method.equals ("Jsonmap")) {//Set encoding format otherwise it will display garbled response.setcontenttype ("Text/html;charset=utf            -8 ");            Map<string,person> MP = new hashmap<string,person> ();            Mp.put ("admin", new person (1001, "Zhang San", "Male", "Guangxi"));            Mp.put ("Visitor", new person (1002, "Zhang Si", "male", "Guangxi"));            Jsonarray Jsonmap = Jsonarray.fromobject (MP);        Send data Response.getwriter (). println (Jsonmap); }    }}

2018.7.27 JSON and Java to each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.