Jackson parsing JSON method in the "736C677C4" Spring Framework __div

Source: Internet
Author: User
From a person on the CSDN blog relay, his blog address is: http://blog.csdn.net/wyc_cs/article/details/6679722

Package Com.bbs.utils.json;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.LinkedHashMap;
Import java.util.List;
Import Java.util.Map;

Import Java.util.Set;
Import Org.codehaus.jackson.JsonGenerator;
Import org.codehaus.jackson.JsonParseException;
Import org.codehaus.jackson.map.JsonMappingException;
Import Org.codehaus.jackson.map.ObjectMapper;

Import Org.codehaus.jackson.node.JsonNodeFactory;
 public class Jacksontest {private static jsongenerator jsongenerator = null;
 private static Objectmapper objectmapper = null;

 private static user user = null; The public static void Writeentity2json () throws IOException {System.out.println ("uses a Jsongenerator transform entity as a JSON string-------------
  ");
  WriteObject can convert Java objects, Eg:javabean/map/list/array, and other jsongenerator.writeobject (user);
  System.out.println ();
  System.out.println ("Use Objectmapper-----------"); WriteValue has the same functionality as WriteObject Objectmapper.writevalue (System. out, user);
  public static void Writelist2json () throws IOException {list<user> userlist = new arraylist<user> ();
  Userlist.add (user);
  User U = new user ();
  U.setuid (10);
  U.setuname ("Archie");
  U.setupwd ("123");
  Userlist.add (U);
 Objectmapper.writevalue (System.out, userlist);
   public static void Writemap2json () {try {map<string, object> Map = new hashmap<string, object> ();
   User U = new user ();
   U.setuid (10);
   U.setuname ("Archie");
   U.setupwd ("123");
   Map.put ("UID", U.getuid ());
   Map.put ("Uname", U.getuname ());
   Map.put ("Upwd", U.getupwd ());
   System.out.println ("Jsongenerator");
   Jsongenerator.writeobject (map);
  Objectmapper.writevalue (System.out, map);
  catch (IOException e) {e.printstacktrace ();
   The public static void Writeothersjson () {try {string[] arr = {"A", "B", "C"};
   System.out.println ("Jsongenerator");
   String str = "Hello World jackson!"; Byte Jsongenerator.writebinary (Str.getbytes ());
   Boolean Jsongenerator.writeboolean (True);
   Null jsongenerator.writenull ();
   Float Jsongenerator.writenumber (2.2f);
   Char Jsongenerator.writeraw ("C");
   String Jsongenerator.writeraw (str, 5, 10);
   String Jsongenerator.writerawvalue (str, 5, 5);
   String jsongenerator.writestring (str);
   Jsongenerator.writetree (JsonNodeFactory.instance.POJONode (str));
   System.out.println (); Object jsongenerator.writestartobject ();//{Jsongenerator.writeobjectfieldstart ("user");//User:jsongenerato R.writestringfield ("name", "Jackson");//Name:jackson Jsongenerator.writebooleanfield ("Sex", true);//Sex:true Jsong
   Enerator.writenumberfield ("Age");//Age:22 jsongenerator.writeendobject ();
   Jsongenerator.writearrayfieldstart ("Infos");//infos:[Jsongenerator.writenumber (22);//22 Jsongenerator.writestring ("This is Array");/This is array jsongenerator.writeendarray ();//] Jsongenerator.writeenDobject ()//} User U = New user ();
   User.setuid (5);
   User.setuname ("Tom");
   User.setupwd ("123");
   User.setnumber (3.44); Complex Object Jsongenerator.writestartobject ()//{Jsongenerator.writeobjectfield ("UID", u);//User:{bean} J Songenerator.writeobjectfield ("Infos", arr);//Infos:[array] Jsongenerator.writeendobject ();/}} catch (Exception E
  ) {e.printstacktrace (); /** * JSON string converted to object/public static void Readjson2entity () {String json = "{\ uid\": 5,\ "uname\": \ "tom\", \ "n
  Umber\ ": 3.44,\" upwd\ ": \" 123\ "}";
   try {User acc = objectmapper.readvalue (JSON, user.class);
   System.out.println (Acc.getuid ());
  SYSTEM.OUT.PRINTLN (ACC);
  catch (Jsonparseexception e) {e.printstacktrace ();
  catch (Jsonmappingexception e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); }/** * JSON is converted to list object/public static void Readjson2list () {String json = ' [{\ uid\ ': 1,\ ' uname\ ': \ ' www\ ', \ ' n Umber\ ": 234, \ "upwd\": \ "456\"}, "+" {\ "uid\": 5,\ "uname\": \ "tom\", \ "Number\": 3.44,\ "upwd\": \ "123\"}] ";
   try {list<linkedhashmap<string, object>> List = Objectmapper.readvalue (JSON, list.class);
   System.out.println (List.size ());
    for (int i = 0; i < list.size (); i++) {map<string, object> Map = List.get (i);
    set<string> set = Map.keyset (); for (iterator<string> it = Set.iterator (); It.hasnext ();)
     {String key = It.next ();
    SYSTEM.OUT.PRINTLN (key + ":" + map.get (key));
  A catch (Jsonparseexception e) {e.printstacktrace ());
  catch (Jsonmappingexception e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); }/** * JSON transforms an array object/public static void Readjson2array () {String json = ' [{\ uid\ ': 1,\ ' uname\ ': \ ' www\ ', \ ' Nu
  Mber\ ": 234,\" upwd\ ": \" 456\ "}," + "{\" uid\ ": 2,\" uname\ ": \" sdfsdf\ ", \" Number\ ": 4745,\" upwd\ ": \" 23456\ "}]"; try {user[] arr = Objectmapper.readvalue (JSON,User[].class);
   System.out.println (arr.length);
   for (int i = 0; i < arr.length i++) {System.out.println (arr[i]);
  } catch (Jsonparseexception e) {e.printstacktrace ();
  catch (Jsonmappingexception e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); }/** * JSON converted to map object/public static void Readjson2map () {String json = ' {\ success\ ': true,\ ' a\ ': {\ ' address\ ":" "address2\", \ "name\": \ "haha2\", "id\": 2,\ "email\": \ "Email2\"}, "+" \ "b\": {\ "address\": \ "Address\", \ "name\": \ "
  Haha\ ", \" id\ ": 1,\" email\ ": \" Email\ "}";
   try {map<string, map<string, object>> maps = Objectmapper.readvalue (JSON, map.class);
   System.out.println (Maps.size ());
   set<string> key = Maps.keyset ();
   Iterator<string> iter = Key.iterator ();
    while (Iter.hasnext ()) {String field = Iter.next ();
   System.out.println (Field + ":" + maps.get (field));
  } catch (Jsonparseexception e) {e.printstacktrace (); } CATCH (jsonmappingexception e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace ();
  } public static void Main (string[] args) {user = new user ();
  User.setuid (5);
  User.setuname ("Tom");
  User.setupwd ("123");
  User.setnumber (3.44);
  Objectmapper = new Objectmapper ();
   try {//jsongenerator = Objectmapper.getjsonfactory (). Createjsongenerator (System.out, Jsonencoding.utf8);
   Writeentity2json ();
   Writemap2json ();
   Writelist2json ();
   Writeothersjson ();
   Readjson2entity ();
Readjson2list ();
   Readjson2array ();
  Readjson2map ();
  catch (Exception e) {e.printstacktrace ();

}} ========================user.java============== package Com.bbs.utils.json;
 public class User {private int uid;
 Private String uname;
 Private String upwd;
 private double number;
 Public double GetNumber () {return number;
 public void Setnumber (double) {this.number = number; public int Getuid () {returnUid
 The public void setUid (int uid) {this.uid = UID;
 Public String Getuname () {return uname;
 } public void Setuname (String uname) {this.uname = uname;
 Public String getupwd () {return upwd;
 } public void Setupwd (String upwd) {this.upwd = upwd; }
 
}

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.