/*** 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