Tutorial on converting data into json encoding format using java

Source: Internet
Author: User
Tags date1 id3 json

It is a lightweight data storage and transmission language.

Json-related packages need to be introduced in java, and the packaging method can be implemented under the lib of the project.

JSON and JAVA Data conversion (JSON is JavaScript Object Natation, which is a lightweight data exchange format, not

It is often suitable for data interaction between servers and JavaScript)

The json expression is similar to the declaration method of the original JavaScript class.

The code is as follows: Copy code
Var obj = {
Name: "javascript ",
Age: 20
};

 

Converts java to a json encoding expression;

1. Create a basic json object

Json code

String str = "{}"; // to convert java to json, you must first create the data to be converted.

JSONObject jsonobj = new JSONObject. fromObject (str); // replace java data with json encoding.

2. Use for to loop several numbers into the created json object.

 

The code is as follows: Copy code
For (int I = 0; I <5; I ++ ){
Jsonobj. put ("json" + I, I); // place java data in a json object
// Jsonobj. put ("json" + I, I); the execution result is the same as that of put.
// Jsonobj. accumulate ("json" + I, I); the execution result is the same as that of put.
System. out. println (jsonobj. toString (); // Print json data
 }

 

Json code

Result:

{"Id0": 0}
{"Id0": 0, "id1": 1}
{"Id0": 0, "id1": 1, "id2": 2}
{"Id0": 0, "id1": 1, "id2": 2, "id3": 3}
{"Id0": 0, "id1": 1, "id2": 2, "id3": 3, "id4": 4}

3. put a JSONObject object to json using put.

Json code

 

The code is as follows: Copy code
For (int I = 0; I <5; I ++ ){
Jsonobj. put ("json" + I, I); // place java data in a json object
// Jsonobj. put ("json" + I, I); the execution result is the same as that of put.
// Jsonobj. accumulate ("json" + I, I); the execution result is the same as that of put.
System. out. println (jsonobj. toString (); // Print json data
 }

 

Result:

{"Id0": 0, "id1": 1, "id2": 2, "id3": 3, "id4": 4, "jsonobj": {"date0 ": 0 }}
{"Id0": 0, "id1": 1, "id2": 2, "id3": 3, "id4": 4, "jsonobj": {"date0 ": 0, "date1": 1 }}
{"Id0": 0, "id1": 1, "id2": 2, "id3": 3, "id4": 4, "jsonobj": {"date0 ": 0, "date1": 1, "date2": 2 }}
{"Id0": 0, "id1": 1, "id2": 2, "id3": 3, "id4": 4, "jsonobj": {"date0 ": 0, "date1": 1, "date2": 2, "date3": 3 }}
{"Id0": 0, "id1": 1, "id2": 2, "id3": 3, "id4": 4, "jsonobj": {"date0 ": 0, "date1": 1, "date2": 2, "date3": 3, "date4": 4 }}

4. Add the array to json.

Java code

 

The code is as follows: Copy code
// Create a basic json object
String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date );
 
// Use int to place the value in json
For (int j = 0; j <5; j ++ ){
// Jsonobj. put ("id" + j, j );
// Jsonobj. accumulate ("id" + j, j );
Jsonobj. element ("id" + j, j );
// System. out. println (jsonobj. toString ());
}      

   

 

Result:

{"Arr": ["json", "ajax", "Jquery", "javascript"]}

5. Put the list in json.

Java code

 

The code is as follows: Copy code

String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date );

ArrayList <String> list = new ArrayList <String> ();
List. add ("json ");
List. add ("java ");
List. add ("android ");
Jsonobj. put ("jsonobj", list );
System. out. println (jsonobj. toString ());

 

Result: {"jsonobj": ["json", "java", "android"]}

6. Put the map in json.

Json code

 

The code is as follows: Copy code
String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date); // create the converted object
Map <String, Object> map = new HashMap <String, Object> (); // create a map queue
Map. put ("map1", "json ");
Map. put ("map2", "java ");
Map. put ("map3", "android ");
Jsonobj. put ("map", map); // put it in json
System. out. println (jsonobj. toString ());

 

Result:

{"Map": {"map3": "android", "map2": "java", "map1": "json "}}

7. Put json into the queue using putAll

Json code

 

The code is as follows: Copy code
String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date); // create the converted object
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("map1", "json ");
Map. put ("map2", "android ");
Map. put ("map3", "Jquery ");
Jsonobj. putAll (map );
System. out. println (jsonobj. toString ());

 

Result:

{"Map3": "Jquery", "map2": "android", "map1": "json "}

8. Differences Between put and putAll:

Put; there will be an object {"map": {"map3": "android", "map2": "java", "map1": "json "}}

PutAll does not {"map3": "Jquery", "map2": "android", "map1": "json "}

9. Combination: (The Returned result value is an array of objects)

Json code

 

The code is as follows: Copy code
// Train of thought:
// 1. Create an object converted to json
// 2. Create An ArrayList queue to store the converted json object.
// 3. Create a json object three times using the for loop and add a value to the json object created in the loop.
// 4. Place the json object created in the loop to the queue.
// 5. Add the queue to json
String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date); // create the converted object
ArrayList <JSONObject> list = new ArrayList <JSONObject> (); // Create a queue
For (int I = 0; I <3; I ++) {// Loop
JSONObject json = JSONObject. fromObject (date); // converted java
Json. put ("aaaa" + I, I );
Json. put ("bbbb" + I, I );
Json. put ("cccc" + I, I );
List. add (json );
    }   
Jsonobj. put ("succList", list );
System. out. println (jsonobj. toString ());

 

Result:

{"SuccList ":[
{"Aaaa0": 0, "bbbb0": 0, "cccc0": 0 },
{& Quot; aaaa1 & quot;: 1, & quot; bbbb1 & quot;: 1, & quot; ccpc3 & quot;: 1 },
{"Aaaa2": 2, "bbbb2": 2, "cccc2": 2}
]}

10. json combination. The returned result value is an array.

Json code

 

The code is as follows: Copy code
// Create a basic json object
String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date );
String [] str = {"json", "ajax", "Jquery", "javascript"}; // Array
Jsonobj. put ("arr", str );
ArrayList <JSONObject> list = new ArrayList <JSONObject> (); // Create a queue
For (int I = 0; I <3; I ++) {// Loop
JSONObject json = JSONObject. fromObject (date); // converted java
Json. put ("aaaa" + I, I );
Json. put ("bbbb" + I, I );
Json. put ("cccc" + I, str); // add an array
List. add (json );
   }   
Jsonobj. put ("succList", list );
System. out. println (jsonobj. toString ());

 

Result:

{"Arr": ["json", "ajax", "Jquery", "javascript"],
"SuccList": [{"aaaa0": 0, "bbbb0": 0, "cccc0": ["json", "ajax", "Jquery ", "javascript"]},
{"Aaaa1": 1, "bbbb1": 1, "ccpc3": ["json", "ajax", "Jquery", "javascript"]},
{"Aaaa2": 2, "bbbb2": 2, "cccc2": ["json", "ajax", "Jquery", "javascript"]}
]}

11. Combination, any nesting;

Json code

 

The code is as follows: Copy code
// Create a basic json object
String date = "{}";
JSONObject jsonobj = JSONObject. fromObject (date );
String [] str = {"json", "ajax", "Jquery", "javascript"}; // Array
// Convert the json of java to be nested into json
JSONObject jsonDemo = JSONObject. fromObject (date );
For (int n = 0; n <3; n ++ ){
JsonDemo. put ("combination" + n, n );
    } 
ArrayList <JSONObject> list = new ArrayList <JSONObject> (); // Create a queue
For (int I = 0; I <3; I ++) {// Loop
JSONObject json = JSONObject. fromObject (date); // converted java
Json. put ("aaaa" + I, jsonDemo); // nested json
Json. put ("bbbb" + I, "java ");
Json. put ("cccc" + I, str); // The value is an array.
List. add (json );
    }   
Jsonobj. put ("succList", list );
System. out. println (jsonobj. toString ());

 

Running result:

{"SuccList ":
[{"Aaaa0": {"combination 0": 0, "combination 1": 1, "combination 2": 2 },
"Bbbb0": "java", "cccc0": ["json", "ajax", "Jquery", "javascript"]},
{"Aaaa1": {"combination 0": 0, "combination 1": 1, "combination 2": 2 },
"Bbbb1": "java", "ccpc3": ["json", "ajax", "Jquery", "javascript"]},
{"Aaaa2": {"combination 0": 0, "combination 1": 1, "combination 2": 2 },
"Bbbb2": "java", "cccc2": ["json", "ajax", "Jquery", "javascript"]}
]}

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.