Fastjson in Java background convert JSON format data (II.)--processing array/list/map__js

Source: Internet
Author: User
Tags goto

Directly to the code:

Package fastjson.test;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import Com.alibaba.fastjson.JSON;

Import Com.alibaba.fastjson.JSONArray; public class FastJsonTest1 {/** * array JSON format string */public void Array2json () {string[] arr = {"b
        Ill "," green "," Maks "," Jim "};
        String Jsontext = json.tojsonstring (arr, true);
        System.out.println ("Array2json () Method: jsontext==" +jsontext);
        Output results: jsontext==["Bill", "Green", "Maks", "Jim"]}/** * JSON format string to array/public void Json2array () {
        String jsontext = "[\ bill\", \ "green\", \ "maks\", \ "jim\"] ";
        Jsonarray Jsonarr = Json.parsearray (Jsontext);
        System.out.println ("Json2array () Method: jsonarr==" +jsonarr); Output results: jsonarr==["Bill", "Green", "Maks", "Jim"]}/** * Array to JSON format string */public void Array2json2 (
        {User User1 = new User ("P001", "TOM", 16); User User2 = New User ("P002 "," JACKSON ", 21);
        User User3 = New User ("P003", "MARTIN", 20);
        User[] Userarr = {User1,user2,user3};
        String Jsontext = json.tojsonstring (Userarr, true);
        System.out.println ("Array2json2 () Method: jsontext==" +jsontext); Output results: jsontext==[{"age": "id": "P001", "name": "TOM"},{"Age": "id": "P002", "name": "JACKSON"},{"Age": "id": " P003 "," name ":" MARTIN "}]}/** * JSON format string to array */public void Json2array2 () {String Jsont ext = "[{\" age\ ": 16,\" id\ ": \" p001\ ", \" name\ ": \" tom\ "},{\" age\ ": 21,\" id\ ": \" p002\ ", \" name\ ": \" jackson\ "},{\" age\ "
        : 20,\ "id\": \ "p003\", \ "name\": \ "martin\"}] ";
        Jsonarray Jsonarr = Json.parsearray (Jsontext);
        System.out.println ("Json2array2 () Method: jsonarr==" +jsonarr); Output results: jsonarr==[{"age": "id": "P001", "name": "TOM"},{"Age": "id": "P002", "name": "JACKSON"},{"Age": "id": " P003 "," name ":" MARTIN "}]}/** * List set goto JSON format string/public void List2json () {List List =New ArrayList ();
        User User1 = new User ("L001", "TOM", 16);
        List.add (user1);
        User User2 = New User ("L002", "JACKSON", 21);
        List.add (User2);
        User User3 = New User ("L003", "MARTIN", 20);
        List.add (USER3);
        String jsontext = json.tojsonstring (list, true);
        System.out.println ("List2json () Method: jsontext==" +jsontext); Output results: jsontext==[{"age": "id": "L001", "name": "TOM"},{"Age": "id": "L002", "name": "JACKSON"},{"Age": "id": "  L003 "," name ":" MARTIN "}]}/** * List set goto JSON format string/public void List2json2 () {List List
        = new ArrayList ();
        Address Address1 = new address ("Guangdong province", "Shenzhen", "Ke Yuan nan lu", "580053");
        User User1 = new User ("L001", "TOM", 16,address1);
        List.add (user1);
        Address address2 = new address ("Jiangxi province", "Nanchang", "Yangmn Road", "330004");
        User User2 = New User ("L002", "JACKSON", 21,address2);
        List.add (User2);
 Address ADDRESS3 = new address ("Shaanxi province", "Xi ' an city", "Long Annan Road", "710114");       User User3 = New User ("L003", "MARTIN", 20,ADDRESS3);
        List.add (USER3);
        String jsontext = json.tojsonstring (list, true);
        System.out.println ("List2json2 () Method: jsontext==" +jsontext); Output result: jsontext==[{"address": "The City": "Shenzhen", "POST": "580053", "Province": "Guangdong Province", "Street": "Ke Yuan nan lu"}, "Age": "," id ":" L001 "," Name ":" "TOM"},{"address": {"City": "Nanchang", "POST": "330004", "Province": "Jiangxi Province", "Street": "Yangmn Road"}, "Age": "," id ":" L002 "," name ":" JACKSON "},{" Address ":" The City ":" Xi ' an "," POST ":" 710114 "," Province ":" Shaanxi Province "," Street ":" Long Annan Road "}," age ": 20,"  ID ":" L003 "," name ":" MARTIN "}]}/** * MAP to JSON format string */public void Map2json () {Map map = new
        HashMap ();
        Address Address1 = new address ("Guangdong province", "Shenzhen", "Ke Yuan nan lu", "580053");
        Map.put ("Address1", Address1);
        Address address2 = new address ("Jiangxi province", "Nanchang", "Yangmn Road", "330004");
        Map.put ("Address2", address2);
        Address ADDRESS3 = new address ("Shaanxi province", "Xi ' an city", "Long Annan Road", "710114");
  Map.put ("Address3", ADDRESS3);      String Jsontext = json.tojsonstring (map, true);
        System.out.println ("Map2json () Method: jsontext==" +jsontext); Output results: jsontext=={"Address1": {"City": "Shenzhen", "POST": "580053", "Province": "Guangdong Province", "Street": "Ke Yuan South"}, "Address2": "City ":" "Nanchang", "POST": "330004", "Province": "Jiangxi Province", "Street": "Yangmn Road"}, "Address3": {"City": "Xi ' an", "POST": "710114", "Province": " Shaanxi Province ", Street": "Long Annan Road"}} public static void Main (string[] args) {FastJsonTest1 test =
        New FastJsonTest1 ();
        
        Array to JSON format string Test.array2json ();
        
        JSON format string to array Test.json2array ();
        
        Array to JSON format string Test.array2json2 ();
        
        JSON format string to array test.json2array2 ();
        
        The list sets the JSON format string Test.list2json ();
        
        The list sets the JSON format string Test.list2json2 ();
    Map to JSON format string Test.map2json ();
 }
}

Console output Results:

Array2json () Method: jsontext==["Bill", "Green", "Maks", "Jim"] Json2array () Method: jsonarr==["Bill", "Green", "Maks", "Jim" Array2json2 () Method: jsontext==[{"Age": "id": "P001", "name": "TOM"},{"Age": "id": "P002", "name": "JACKSON"},{"a GE ": id": "P003", "name": "MARTIN"}] Json2array2 () Method: jsonarr==[{"Age": "id": "P001", "name": "TOM"},{"age": 21, " ID ":" P002 "," name ":" JACKSON "},{" Age ":" id ":" P003 "," name ":" MARTIN "}] List2json () method: jsontext==[{" Age ":", "id": " L001 "," name ":" TOM "}, {" Age ":" id ":" L002 "," name ":" JACKSON "}, {" Age ":" id ":" L003 "," name ":" MAR TIN "}] List2json2 () method: jsontext==[{" Address ": {" City ":" Shenzhen "," POST ":" 580053 "," Province ":" Guangdong Province "," Street ":" Ke Yuan nan Road "}," Age ":" id ":" L001 "," name ":" TOM "}, {' address ': {" City ":" Nanchang "," POST ":" 330004 "," Prov
			Ince ":" Jiangxi Province "," Street ":" Yangmn Road "}," Age ":" id ":" L002 "," name ":" JACKSON "}, {" Address ":" The City ":" Xi ' an ", "POST": "710114", "Province": "Shaanxi Province", "StreeT ":" Long Annan Road "}," Age ":" id ":" L003 "," name ":" MARTIN "}] Map2json () method: jsontext=={" Address1 ": {" City ":" Shenzhen "," Pos T ":" "580053", "Province": "Guangdong Province", "Street": "Branch Garden South"}, "Address2": {"City": "Nanchang", "POST": "330004", "Province": "Jiangxi Province", "Stre" ET ":" Yangmn Road "}," Address3 ": {" City ":" Xi ' an "," POST ":" 710114 "," Province ":" Shaanxi Province "," Street ":" Long Annan Road "}


Attached: JavaBean class User.java

Package fastjson.test;

Import java.io.Serializable;
	
	public class User implements Serializable {private static final long serialversionuid = 1L;
	Private String ID;
	private String name;
    private int age;
	
	private address address;
	Public User () {super ();
		Public User (string ID, string name, int age) {super ();
		This.id = ID;
		THIS.name = name;
	This.age = age;
        Public User (string ID, string name, int age, address address) {super ();
        This.id = ID;
        THIS.name = name;
        This.age = age;
    this.address = address;
	public int getage () {return age;
	public void Setage (int age) {this.age = age;
	Public String GetId () {return id;
	public void SetId (String id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
    Public Address getaddress () {return address; } public void Setaddress (address address) {this.address = address;
 }
	
}

Address.java

Package fastjson.test;

Import java.io.Serializable;
    
    Public class address implements Serializable {private static final long serialversionuid = 1L;
    Private String Province;
    Private String City;
    Private String Street;
    
    Private String Post;
    Public address () {super ();
        (String province, String city, String Street, String post) {super ();
        this.province = Province;
        this.city = City;
        This.street = Street;
    This.post = post;
    Public String getcity () {return city;
    public void Setcity (String city) {this.city = city;
    Public String Getpost () {return post;
    public void Setpost (String post) {this.post = post;
    Public String getprovince () {return province;
    } public void Setprovince (String province) {this.province = province; } public String Getstreet () {return street;
    public void Setstreet (String street) {this.street = Street;
 }
    
}

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.