Using Jackson to deserialize in Java

Source: Internet
Author: User

Build.gradle:

Compile group: ' Org.codehaus.jackson ', Name: ' JACKSON-MAPPER-LGPL ', version: ' 1.9.13 '
Compile group: ' Org.codehaus.jackson ', Name: ' JACKSON-CORE-LGPL ', version: ' 1.0.1 '

Compile group: ' HttpUnit ', Name: ' HttpUnit ', Version: ' 1.3 '

Send WebRequest related packages, such as the Jackson Bag and webconversation/webresponse, respectively. Click this refresh to add a reference.

Call request in Main

// interface of the API //Normalservice is the deserialization class Normalservice = (normalservice) testused to receive the API .                 Callresetapi (Class.forName ("Normalservice"), URL, "GET");

Below is the CALLRESETAPI section, which uses generics in Java, and generics for Java refer to http://www.cnblogs.com/iyangyuan/archive/2013/04/09/3011274.html

Import Com.meterware.httpunit.webconversation;import Com.meterware.httpunit.webresponse;import org.codehaus.jackson.map.objectmapper;/** * Created by Ygshen on 2015/3/3. */public class Test {public    static <T> T Callresetapi (class<t> tclass, final string url, string method) T Hrows Exception {        webconversation conversation = new Webconversation ();        WebResponse response = conversation.getresponse (URL);        String Respstream = Response.gettext ();        Return ReadObject (                tclass,                respstream);    }    private static <T> T ReadObject (            class<t> tclass,            String stream) throws Exception {        t data = tCl Ass.newinstance ();        try {            data = new Objectmapper (). ReadValue (Stream,tclass);        } catch (Exception e) {            //Handle the PROBLEM
   throw e;        }        return data;}    }

The following is the deserialized class code

Import Java.io.serializable;import java.util.list;/** * Created by Ygshen on 2015/3/3. */public class Normalservice implements serializable{public    list<service> servicelist;    public int count;    Public String env;    public Boolean success;} Service section Code import java.io.serializable;import java.util.list;/** * Created by Ygshen on 2015/3/3. */public class Service implements serializable{public    String Servicecode;    Public String serviceName;    Public String servicenamespace;    Public String servicecontacts;    Public list<serviceendpoint> endpointlist;} /** * Created by Ygshen on 2015/3/3. */public class ServiceEndpoint {public    String Test1;    Public String Test2;    Public String Test3;}

  

This allows you to test the output in the main function as soon as possible.



Using Jackson to deserialize in Java

Related Article

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.