Conversion of json,string and object __js

Source: Internet
Author: User
Tags object object

Now the interface data for the mainstream platform is already in JSON format. A simple introduction to centralized conversion is the use of Third-party jar packages.

1. Jackson.

Official website: http://jackson.codehaus.org/The Jackson-all-1.9.1.jar http://pan.baidu.com/s/1gd87aGr usage I'm using

private static Objectmapper Objectmapper = new Objectmapper ();
    static{        Objectmapper.setdateformat (New SimpleDateFormat ("Yyyy-mm-dd"));         Objectmapper.setdeserializationconfig (Objectmapper.getdeserializationconfig (). Without (                                   Des
ErializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));
   }    /**      * using objects for JSON deserialization.      * @param json JSON string      * @param pojoclass class type JSON string type to class      *  /    public static Object Decodejson (String json, Class Pojoclass) throws exception{             try{            return Objectmapper.readvalue (JSON, pojoclass); &nbsp ;      }catch (Exception e) {            Throw E
       }     {   /**      * serialization of objects. 
     * @param o entity object      * @return serialized JSON object to string, convert map and other types      */     public static String Encodejson (Object o) throws exception{        try{   
        return objectmapper.writevalueasstring (o);        }catch (Exception e) {            throw e;       & nbsp
 }    }

2. Fastjson

Alibaba Fastjson is also a Json toolkit, the fastest package, is said to test beyond other Java JSON parser, beyond the Jackjson, support Java beans, collections, maps, dates, enums, support generics support introspection ; able to run directly in the Java environment, support Android, and open source.

API entry classes are Com.alibaba.fastjson.JSON common operations

public static final Object parse (String text); Parse the JSON text to Jsonobject or Jsonarray public 
static final Jsonobject parseobject (String text); Parse the JSON text into Jsonobject public    
static final  T parseobject (String text, Class clazz);//parse JSON text as javabean< C3/>public static final Jsonarray Parsearray (String text); Parse the JSON text into Jsonarray public 
static final  List Parsearray (String text, Class clazz); Parse the JSON text into the JavaBean collection public 
static final String tojsonstring (object);//convert JavaBean to JSON text public 
Static final String tojsonstring (Object object, Boolean Prettyformat); Serializes the JavaBean to the formatted JSON text public 
static final object Tojson (object javaobject); Converts the JavaBean to Jsonobject or Jsonarray.

My jar package: http://pan.baidu.com/s/1dDxfK21 plus a request parsing, parsing is done directly into Java classes,

private string Getrequestbody (HttpServletRequest request) throws IOException {
string postbody = new string ();
BufferedReader Breader = Request.getreader ();
        String line = null;
        while (line = Breader.readline ())!= null) {
            postbody + = line;
        }
return postbody;
}
Pay Ucreq = Json.parseobject (Postbody, Pay.class);


3.Gson

Google's Gson package, my jar bag.

	    Jsonparser json = new Jsonparser ();
            Jsonobject obj = (jsonobject) json.parse (content);
            String orderId = Obj.get ("OrderId"). toString ();
            String money = obj.get (' money '). toString ();

4.json.

Two packages required Json-smart.jarjson-lib-2.4-jdk15.jar

Jsonparser jsonparser = new Jsonparser (jsonparser.default_permissive_mode);
   Jsonobject obj;
   try {
       obj = (jsonobject) jsonparser.parse (JSONSTR);
   } catch (ParseException e) {
       throw new qexception ( Qexception.code_json_error, jsonstr);
   catch (Exception E1) {
       throw new Qexception (Qexception.code_json_error, jsonstr);
   }
   String errorcode = (string) obj.get ("Error_code");



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.