Jackson simple to use, object to Json,json to object, JSON to list

Source: Internet
Author: User

Add Jackson Dependency:

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-coreCompile group: ' Com.fasterxml.jackson.core ', Name: ' Jackson-core ', version: ' 2.8.2 '//https://mvnrepository.com/ Artifact/com.fasterxml.jackson.core/jackson-databindCompile group: ' Com.fasterxml.jackson.core ', Name: ' Jackson-databind ', version: ' 2.8.2 '//https://mvnrepository.com/artifact/com.fasterxml.jackson.core/ Jackson-annotationsCompile group: ' Com.fasterxml.jackson.core ', Name: ' Jackson-annotations ', version: ' 2.8.2 '

See Fasterxml also thought to find the wrong reliance, also thought and Com.alibaba:fastjson This has what connection, thought is a person called Jack wrote. There are three dependencies, and it's natural to take it for granted that most frameworks rely on Jackson to handle JSON conversions.

Pojo serialized as JSON string:

Prepare a Pojo:

@JsonIgnoreProperties (Ignoreunknown =true)classUserImplementsSerializable {Private Static Final LongSerialversionuid = -5952920972581467417l; PrivateString name;  PublicUser () {} PublicUser (String name) { This. Name =name; }     PublicString GetName () {returnname; } @Override PublicString toString () {return"user{" + "name=" + Name + '} '; }}
    • @JsonIgnoreProperties (Ignoreunknown = True) is for deserialization, if you encounter a filed that you do not know, ignore it
    • The parameterless constructor is for the time that Jackson can create an Pojo instance when deserializing
    • The Getter method is for serialization, and Jackson can get the filed value
    • ToString is handy for my own debug look show
    • As for serializable, the habit of adding a persistent ability to the entity.

Convert to Jason string by write:

String expected = "{\" name\ ": \" test\ "}"= mapper.writevalueasstring (new User ("Test")); Assert.assertequals (expected, test);

Parse JSON string as Pojo object by read:

User user = Mapper.readvalue (expected, user.  Class); Assert.assertequals ("Test", User.getname ());

Jsonarray converted to array arrays:

String expected = "[{\" name\ ": \" ryan\ "},{\" name\ ": \" test\ "},{\" name\ ": \" Leslie\ "}]"= Mapper.gettypefactory (). Constructarraytype (User.  Class= Mapper.readvalue (expected, arraytype); Assert.assertequals ("Ryan", Users[0].getname ());

Jsonarray converted to list<> generics:

Expected= "[{\" a\ ": 12},{\" b\ ": 23},{\" name\ ": \" ryan\ "}]"= Mapper.gettypefactory (). Constructcollectiontype (ArrayList.  Class, User. class ); // The sieze of the list is Dependon the str JSON length although the JSON content are not the POJO type maybe List<user> userlist = Mapper.readvalue (expected, listtype); Assert.assertequals (3, Userlist.size ()); Assert.assertnull (Userlist.get (0). GetName ()); Assert.assertequals ("Ryan", Userlist.get (2). GetName ());

Jackson By default converts an object to Linkedhashmap:

String expected = "[{\" name\ ": \" ryan\ "},{\" name\ ": \" test\ "},{\" name\ ": \" Leslie\ "}]"= Mapper.readvalue ( Expected, ArrayList. class  = arraylist.get (0instanceof linkedhashmap);

Jackson simple to use, object to Json,json to object, JSON to list

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.