Server-side-front-end JSON data transfer summary

Source: Internet
Author: User
Tags string to json tojson

The Java server receives a front-end string to JSON:

Import Com.alibaba.fastjson.JSONObject; String s = "{\" userloginid\ ": \" sysman\ ", \" mobileids\ ": \" bdzfzl001,sgbj001\ "}"; Jsonobject jsonobj = Json.parseobject (s); System.out.println (Jsonobj.get ("Userloginid")); System.out.println (Jsonobj.get ("Mobileids")); String mobileids = (string) jsonobj.get ("Mobileids"); string[] Arrmobileid = Mobileids.split (","); System.out.println (arrmobileid[0]+ "|" +ARRMOBILEID[1]);


The Java server is sent to the front-end:

Jsonutil.java

package com.zhxjz.framework.util.common;import java.util.arraylist;import java.util.iterator; import java.util.list;import org.apache.commons.lang.stringutils;import  org.apache.commons.logging.log;import org.apache.commons.logging.logfactory;import  org.codehaus.jackson.jsonnode;import org.codehaus.jackson.map.objectmapper;import  Com.zhxjz.framework.model.json.jsonresult;/** * json processing Tools  *  *  @author   Caozj */public class jsonutil {private jsonutil ()  {}private static  Final objectmapper mapper = new objectmapper ();p rivate static final  Log logger = logfactory.getlog (jsonutil.class);/** * object -> json  *  *  @param  obj *  @return  */public static String  ToJson (object obj)  {try {return mapper.writevalueasstring (obj);}  catch  (exception e)  {logger.error ("Object converted to JSON failed",  e);} Return null;} /** * json -> object *  *  @param  <T> *  @param  content *  @param  valueType *  @return  */public static <T>  t toobject (String content, class<t> valuetype)  {if  ( Stringutils.isempty (content))  {return null;} Try {return mapper.readvalue (Content, valuetype);}  catch  (exception e)  {logger.error ("JSON converted to object failed",  e);} Return null;} /** * json -> list *  *  @param  <T> *  @param  content *  @param  valueType *  @return  */public static <T>  list<t> tolist (String content, class<t> valuetype)  {JsonNode  node = null;try {node = Mapper.readtree (content);}  catch  (exception e)  {logger.error ("JSON converted to list failed",  e);} if  (node == null)  {return new ArrayList<T> (0);} Iterator<jsonnode> iterator = node.getelements (); List<t> list = new arraylist<t> ();while  (Iterator.hasNext ())  {String  json = iterator.next (). toString (); T e = toobject (Json, valuetype); List.add (e);} Return list;} /** * success json *  *  @return  - {success:true} */public  static string successjson ()  {return tojson (New jsonresult ());} /** * result json *  *  @param  success *             -  operation Success  *  @param  message *             -  Results Information  *  @return  - {success:true,message: ' Test '} */public static string resultjson (boolean success, string message)  {return tojson (New jsonresult (message,  Success ? jsonresult.successcode : jsonresult.failcode));} /** * result json *  *  @param  code *             -  Status Codes  *  @param  message *             -  results Information  *  @param  data *             -  Results Data  *  @return  * /public static string resultjson (Int code, string message, object data )  {return tojson (New jsonresult (Data, message, code));}}

Jsonresult.java

package com.zhxjz.framework.model.json;import com.zhxjz.framework.util.common.jsonutil;/** *   JSON formatted data returned to the page  *  *  @author  caozj *  */public class  jsonresult {/** *  Success Code  */public static final int successcode =  200;/** *  Error code  */public static final int failcode = 400;/**  *  Status code (200 for success, others to customize)  */private int code;/** *  prompt message  */private  string msg;/** *  data Object  */private object data;public int getcode ()   {Return code;} Public void setcode (Int code)  {this.code = code;} Public string getmsg ()  {return msg;} Public void setmsg (string msg)  {this.msg = msg;} Public object getdata ()  {return data;} Public void setdata (Object data)  {this.daTa = data;} Public jsonresult ()  {code = successcode;} Public jsonresult (Object data)  {this.data = data;code = successcode;} Public jsonresult (object data, string msg)  {this.data = data;this.code  = successcode;this.msg = msg;} Public jsonresult (Object data, string msg, int code)  {this.data =  data;this.code = code;this.msg = msg;} Public jsonresult (String msg, int code)  {this.code = code;this.msg =  msg;} Public jsonresult (Int code)  {this.code = code;} Public string tojson ()  {return jsonutil.tojson (this);}}

list<map<string, object>> dataList = Userfuncservice.getuserfunc (account, Arrmobileid);

String message = new Jsonresult (dataList). ToJson ();


Server-side-front-end JSON data transfer summary

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.