Java Data Transformation (Java and JSON and resolution of map and list) __ Large data

Source: Internet
Author: User

Recently, the entire transmission of data, of course, has been in the JSON format, naturally, the conversion between the format of the data, including JSON to Java Bean object, package parsing map, list and so on. Today we'll probably record these methods for later use.
One, JSON
1. Convert JSON to Java objects
The JSON string is:

String s ={"id": "1", "name": "Shanxi", "librays":
[{"LibraryID": "1", "LibraryName": "Centrallibray", "Bookcount": " 10021 "}

Declaring entity classes

public class City {

    private int id;
    private String name;
    Private list<library> librays;
    public int getId () {return
        ID;
    }
    public void setId (int id) {
        this.id = ID;
    }
    Public String GetName () {return
        name;
    }
    public void SetName (String name) {
        this.name = name;
    }
    Public list<library> Getlibrays () {return
        librays;
    }
    public void Setlibrays (list<library> librays) {
        this.librays = librays;
    }

}
public class Library {

    private int libraryid;
    Private String LibraryName; 
    Private String Bookcount;
    public int Getlibraryid () {return
        LibraryID;
    }

    Public String Getbookcount () {return
        bookcount;
    }
    public void Setbookcount (String bookcount) {
        this.bookcount = Bookcount;
    }
    public void Setlibraryid (int libraryid) {
        This.libraryid = LibraryID;
    }
    Public String Getlibraryname () {return
        libraryname;
    }
    public void Setlibraryname (String libraryname) {
        this.libraryname = libraryname;
    }
}

Implementation: This has two steps, one step is to encapsulate the nested JSON (that is, the child entity class into the Classmap), and the second step is to add a JSON to the JavaBean object.

map<string,class> Classmap = new hashmap<string,class> ();
        Classmap.put ("Librays", library.class);

        Jsonobject obj = new Jsonobject (). Fromobject (s);
        City City = Jsonobject.tobean (Obj,city.class,classmap);

In addition to this method in the Json-lib-2.2-jdk15.jar package, Google's Gson is also very flexible to deal with this problem
In addition, there are some basic methods of Jsonobject:
Such as:

Converts a string in the form of a JSON array into a Jsonarray object
jsonarray arr= jsonarray.fromobject (s);
Converts a string in JSON form to a JSON object
jsonobject JSON = Jsonobject.fromobject (s);
Converts a JSON object to a string
json.tostring ();/arr.tostring
//Gets the nth Jsonobject object
arr.getjsonobject (n)
of the Jsonarray; Gets a value
arr.getjsonobject (n). getString (key) for a JSON object;

2. Traversing the JSON object

Traverses the JSON string (JSON object)
jsonobject jsonobject = new Jsonobject ();
Jsonobject = Jsonobject.fromobject (s);

Iterator iterator = Jsonobject.keys ();
while (Iterator.hasnext ()) {
    key = (String) iterator.next ();
    value1 = jsonobject.getstring (key);         
}

3. Traversing the JSON array
Just go through the jsonobject and set a For loop OK
4. In addition,
Note that a Java-defined structured object, such as a map, a JSON object, or an array, cannot be equal to NULL, so that it is not an object of the struct, and therefore some operations of the structure are no longer possible;
A new object, such as the New Jsonarray (), is used to leave the JSON array blank.
Second, map
1. Traverse Map
Method One:

list
Map MAPC = Entry.getvalue (). get (i);
Iterator iter = Mapc.entryset (). iterator ();

 while (Iter.hasnext ()) {
    Map.entry entryc = (Entry) iter.next ();
    System.out.println ("key=" +entryc.getkey () + ", value=" +entryc.getvalue ());
}

2. Merging two maps

for (int i =0;i<map2.size (); i++) {
Map1.add (Map2.get (i));
}

3. Define Map

Map map = new HashMap ();
Map.put ("Key1", "value1");
Map.put ("Key2", "value2");

4. Get the value of the map

Map.get ("Key1");

5. Determine if map is empty
(Map.size=0 and Map.isempty () almost)

if (Map!=null &&!map.isempty ()) {
This inside takes the value in the list
}else{
do other processing
}

Third, List
1. Traverse List

List List = new ArrayList ();
List.get (i);

2. Define List

List.add (i, "value");

3. Modify List

List.set (i, "value");

4. Determine whether the list is empty
if (List!=null &&!list.isempty ()) {
This inside takes the value from the list
}else{
Do other processing
}

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.