Use Jackson to convert the JSON string to array format into list.

Source: Internet
Author: User

There is a string as follows. Below, also through Jackson to convert list to JSON string, I want to turn it over to see the content on the Internet is not satisfactory, are the content of the pieces. It is estimated that only the written know how to use, so directly to see Jackson's official website, know how to use.

The classes used are mainly

Import org.codehaus.jackson.type.TypeReference; import Org.codehaus.jackson.map.ObjectMapper;

The string to be transferred is as follows:

[{"id": "36cd0224c1ed25f5e0538a3b0b7a8190", "Catgid": null, "Matcamont": 50000, "Lendpoolid": " 36cd0224c1c225f5e0538a3b0b7a8190 "," balance ": 50000," Matchflag ": 1," Islock ": 1," Createtime ": 1467576000000," UserId " : 0, "Mltcustlendpool": {"id": "36cd0224c1c225f5e0538a3b0b7a8190", "userId": 157020, "state": 1, "Islock": 1, "balance" : 50000, "Curtotaamt": 50000, "syncdate": 1467561600000, "Inviflag": 2, "Investtime": 1467595065000, "Deadline" : 1499131065000, "billdate": 1467681465000, "Billday": 5, "ProductId": "6", "ProductName": "xxx", "Investamt": 50000, " Prodorderid ": 38662," Usertelephone ":" 15922166933 "," UserName ":" Zhang San "," Idcardnum ":" 1111111111111 "," projectnum ": null , "Matchmodelcode": null, "Creditproduct": {"id": "290aa19b1134838ee053a716c0769130", "Swldid": "6", "ProdName": "XXX", "Prodtype": null, "Prodrate": 0.13, "synfeerate": null, "Paycapitaltype": null, "Freezetime": $, "Prodcode": 6, " Prodcatgory ": 1," unit ": 1}," subject ": null}," Orderflag ": null," Subject ": 0," projectnum ": null," Matchmodecode ":" 1 "},{ "id": "36da5b50e54e2790e0538a3b0b7a2261", "Catgid": null,"Matcamont": 50000, "Lendpoolid": "36cd0224c1c225f5e0538a3b0b7a8190", "balance": 50000, "Matchflag": 1, "Islock": 1, " Createtime ": 1467748800000," userid ": 0," Mltcustlendpool ": {" id ":" 36cd0224c1c225f5e0538a3b0b7a8190 "," userid " : 157020, "state": 1, "Islock": 1, "balance": 50000, "Curtotaamt": 50000, "syncdate": 1467561600000, "Inviflag": 2, " Investtime ": 1467595065000," Deadline ": 1499131065000," billdate ": 1467681465000," Billday ": 5," ProductId ":" 6 "," ProductName ":" xxx "," Investamt ": 50000," Prodorderid ": 38662," Usertelephone ":" 15922166933 "," UserName ":" Wang DD "," Idcardnum ":" 2222222222222 "," projectnum ": null," Matchmodelcode ": null," Creditproduct ": {" id ":" 290aa19b1134838ee053a716c0769130 "," Swldid ":" 6 "," ProdName ":" DFDFD "," Prodtype ": null," Prodrate ": 0.13," synfeerate " : null, "Paycapitaltype": null, "Freezetime": "Prodcode": 6, "prodcatgory": 1, "unit": 1}, "subject": null}, "Orderflag" : null, "Subject": 0, "projectnum": null, "Matchmodecode": "1"}]

The main code is as follows:

        New Objectmapper ();        List<MltWaitLendReco> Lendreco = Mapper.readvalue (liststr,new typereference<list< Mltwaitlendreco>>() {});        System.out.println (Lendreco.get (0). GetId ());

This allows you to convert the JSON string into the desired list.

Note that the new typereference<list<mltwaitlendreco>> () {} in the ReadValue () methodis critical, and you can't write List.class.

One more thing, The Objectmapper class is also available in the bag at the beginning of Com.fasterxml.jackson, which is Org.codehaus.jackson

Here's a tool class that used to transfer objects and JSON to each other.

Importorg.codehaus.jackson.JsonGenerationException;Importorg.codehaus.jackson.JsonParseException;ImportOrg.codehaus.jackson.JsonParser;ImportOrg.codehaus.jackson.map.DeserializationConfig;Importorg.codehaus.jackson.map.JsonMappingException;ImportOrg.codehaus.jackson.map.ObjectMapper;Importorg.codehaus.jackson.type.TypeReference;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importjava.io.IOException; Public classJsonutils {/*** Logger for this class*/    Private Static FinalLogger Logger = Loggerfactory.getlogger (jsonutils.class); Private Final StaticObjectmapper Objectmapper =NewObjectmapper (); Static{objectmapper.configure (JsonParser.Feature.ALLOW_COMMENTS,true); Objectmapper.configure (JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,true); Objectmapper.configure (JsonParser.Feature.ALLOW_SINGLE_QUOTES,true); Objectmapper.configure (JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true); Objectmapper.configure (JsonParser.Feature.INTERN_FIELD_NAMES,true); Objectmapper.configure (JsonParser.Feature.CANONICALIZE_FIELD_NAMES,true); Objectmapper.configure (DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false); }     Public StaticString encode (Object obj) {Try {            returnobjectmapper.writevalueasstring (obj); } Catch(jsongenerationexception e) {logger.error ("Encode (Object)", e);//$NON-nls-1$}Catch(jsonmappingexception e) {logger.error ("Encode (Object)", e);//$NON-nls-1$}Catch(IOException e) {logger.error ("Encode (Object)", e);//$NON-nls-1$        }        return NULL; }    /*** Deserializes a JSON string into an object * *@paramJSON *@paramValueType *@return     */     Public Static<T> T Decode (String JSON, class<t>ValueType) {        Try {            returnObjectmapper.readvalue (JSON, valueType); } Catch(jsonparseexception e) {logger.error ("Decode (String, class<t>)", E); } Catch(jsonmappingexception e) {logger.error ("Decode (String, class<t>)", E); } Catch(IOException e) {logger.error ("Decode (String, class<t>)", E); }        return NULL; }    /*** Deserialize JSON array to object * *@paramJSON *@paramJsontypereference *@return     */     Public Static<T> T Decode (String JSON, typereference<t>jsontypereference) {        Try {            return(T) objectmapper.readvalue (JSON, jsontypereference); } Catch(jsonparseexception e) {logger.error ("Decode (String, jsontypereference<t>)", E); } Catch(jsonmappingexception e) {logger.error ("Decode (String, jsontypereference<t>)", E); } Catch(IOException e) {logger.error ("Decode (String, jsontypereference<t>)", E); }        return NULL; }}

Use Jackson to convert the JSON string to array format into list.

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.