Convert any format to a tool class of JSON data format, json tool class

Source: Internet
Author: User

Convert any format to a tool class of JSON data format, json tool class

Java tool to convert any format to JSON Data Format

Package org. sjw. utils; import java. beans. introspectionException; import java. beans. introspector; import java. beans. propertyDescriptor; import java. util. list; import java. util. map; import java. util. set; public class JsonUtils {public static String stringToJson (String s) {if (s = null) {return nullToJson ();} StringBuilder sb = new StringBuilder (); for (int I = 0; I <s. length (); I ++) {char ch = s. char At (I); switch (ch) {case '"': sb. append ("\" "); break; case '\': sb. append ("\\\\"); break; case '\ B': sb. append ("\ B"); break; case '\ F': sb. append ("\ f"); break; case '\ N': sb. append ("\ n"); break; case '\ R': sb. append ("\ r"); break; case '\ t': sb. append ("\ t"); break; case '/': sb. append ("\/"); break; default: if (ch> = '\ u000000' & ch <=' \ u001f') {String ss = Integer. toHexString (ch); sb. ap Pend ("\ u"); for (int k = 0; k <4-ss. length (); k ++) {sb. append ('0');} sb. append (ss. toUpperCase ();} else {sb. append (ch) ;}} return sb. toString ();} public static String nullToJson () {return "";} public static String objectToJson (Object obj) {StringBuilder json = new StringBuilder (); if (obj = null) {json. append ("\" \ "");} else if (obj instanceof Number) {json. append (numberToJson (N Umber) obj);} else if (obj instanceof Boolean) {json. append (booleanToJson (Boolean) obj);} else if (obj instanceof String) {json. append ("\""). append (stringToJson (obj. toString ())). append ("\" ");} else if (obj instanceof Object []) {json. append (arrayToJson (Object []) obj);} else if (obj instanceof List) {json. append (listToJson (List <?>) Obj);} else if (obj instanceof Map) {json. append (mapToJson (Map <?, ?>) Obj);} else if (obj instanceof Set) {json. append (setToJson (Set <?>) Obj);} else {json. append (beanToJson (obj);} return json. toString ();} public static String numberToJson (Number number) {return Number. toString ();} public static String booleanToJson (Boolean bool) {return bool. toString ();}/*** @ param bean * bean Object * @ return String */public static String beanToJson (Object bean) {StringBuilder json = new StringBuilder (); json. append ("{"); PropertyDescript Or [] props = null; try {props = Introspector. getBeanInfo (bean. getClass (), Object. class ). getPropertyDescriptors ();} catch (IntrospectionException e) {} if (props! = Null) {for (int I = 0; I <props. length; I ++) {try {String name = objectToJson (props [I]. getName (); String value = objectToJson (props [I]. getReadMethod (). invoke (bean); json. append (name); json. append (":"); json. append (value); json. append (",");} catch (Exception e) {}} json. setCharAt (json. length ()-1, '}');} else {json. append ("}");} return json. toString ();}/*** @ param list * list object * @ Return String */public static String listToJson (List <?> List) {StringBuilder json = new StringBuilder (); json. append ("["); if (list! = Null & list. size ()> 0) {for (Object obj: list) {json. append (objectToJson (obj); json. append (",");} json. setCharAt (json. length ()-1, ']');} else {json. append ("]");} return json. toString ();}/*** @ param array * Object array * @ return String */public static String arrayToJson (Object [] array) {StringBuilder json = new StringBuilder (); json. append ("["); if (array! = Null & array. length> 0) {for (Object obj: array) {json. append (objectToJson (obj); json. append (",");} json. setCharAt (json. length ()-1, ']');} else {json. append ("]");} return json. toString ();}/*** @ param map * map object * @ return String */public static String mapToJson (Map <?, ?> Map) {StringBuilder json = new StringBuilder (); json. append ("{"); if (map! = Null & map. size ()> 0) {for (Object key: map. keySet () {json. append (objectToJson (key); json. append (":"); json. append (objectToJson (map. get (key); json. append (",");} json. setCharAt (json. length ()-1, '}');} else {json. append ("}");} return json. toString ();}/*** @ param set * Collection object * @ return String */public static String setToJson (Set <?> Set) {StringBuilder json = new StringBuilder (); json. append ("["); if (set! = Null & set. size ()> 0) {for (Object obj: set) {json. append (objectToJson (obj); json. append (",");} json. setCharAt (json. length ()-1, ']');} else {json. append ("]");} return json. toString ();}}



C # how to convert json data to List <T>? The json data format is as follows:

Public class Model
{
Public int Count {set; get ;}
Public List <Data> Data {set; get ;}
}

Public class Data
{
Public string carLengthWin {set; get ;}
Public string carAxleRequire {set; get ;}
Public string tradeTelephoneNumber {set; get ;}
}
Then use the Deserialize method of JavaScriptSerializer for deserialization. The parameter is json

How to convert background data to json format,

Answer: 30. When the night comes, it is the time when a person's mind is the most fragile, and the time when he misses the crazy.

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.