Java JSON string and object cross-transfer

Source: Internet
Author: User

/*** Created by admin on 2017/7/26.*/ Public classNewpost {PrivateString title; PrivateString content;  Publicnewpost () {} Publicnewpost (String title,string content) {Settitle (title);    SetContent (content); }     PublicString GetTitle () {returntitle; }     Public voidSettitle (String title) { This. title =title; }     PublicString getcontent () {returncontent; }     Public voidsetcontent (String content) { This. content=content; } @Override PublicString toString () {return"newpost{" + "title=" + title + ' \ ' + ", content= ' + content + ' \ ' + '} '; }}
ImportCom.alibaba.fastjson.JSON;Importjava.util.ArrayList;Importjava.util.List;/*** Created by admin on 2017/7/26.*/classTestfastjson {String json1= "[{\" title\ ": \" post1\ ", \" content\ ": \" post1\ "},{\" title\ ": \" post1\ ", \" content\ ": \" post1\ "}]"; String Json2= "{\" title\ ": \" post1\ ", \" content\ ": \" post1\ "}";  Public voidtestfj () {//JSON string goto List Object//list<newpost> list= New arraylist<newpost> ();List listpost =json.parseobject (json1,list.class); System.out.println ("Listpost" +listpost); //JSON string Goto objectNewpost Post =json.parseobject (json2,newpost.class); System.out.println ("POST" +post); //object to JSON stringList<newpost> list=NewArraylist<newpost>(); List.add (NewNewpost ("Post1", "Post1")); List.add (NewNewpost ("Post2", "Post2")); String jsonstring=json.tojsonstring (list); System.out.println ("Jsonstring" +jsonstring); }     Public Static voidMain (string[] args) {Testfastjson tf=NewTestfastjson ();    TF.TESTFJ (); }}

Listpost1 [{"title": "Post1", "Content": "Post1"}, {"title": "Post1", "Content": "Post1"}]
Listpost2 newpost{title= ' post1 ', content= ' Post1 '}
jsonstring [{"Content": "Post1", "title": "Post1"},{"content": "Post2", "title": "Post2"}]

Java JSON string and object cross-transfer

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.