Convert Java basic data to Json code, and convert java data to json
JSON is a lightweight data exchange format, which is very suitable for the interaction between servers and JavaScript. It is the best choice for Data Interaction between data clients and servers in mobile development. Especially in HTML5, it is more important to use Json to complete data interaction. here, I will demonstrate how to convert basic Java data to Json code
Json required package
If you do not want to download the jar file, you can use this demo.
1. Convert the List set to json code
List <Person> list = new ArrayList <Person> ();
list.add (new Person (0, "nagi_ho", 20, "Haidian"));
list.add (new Person (1, "eson_ho", 21, "chaoyang"));
JSONArray jsonList = JSONArray.fromObject (list);
System.out.println ("1.List collection converted into json code ====>" + jsonList.toString ());
2. Convert the Map set to json code
Map map = new HashMap ();
map.put ("name", "json");
map.put ("bool", Boolean.TRUE);
map.put ("int", new Integer (1));
map.put ("arr", new String [] {"btbu", "b"});
map.put ("func", "function (i) {return this.arr;}");
JSONObject jsonMap = JSONObject.fromObject (map);
System.out.println ("2.Map collection converted into json code ====>" + jsonMap.toString ());
3. Convert the Set to json code
Set <Person> set = new HashSet <Person> ();
set.add (new Person (0, "nagi_ho", 20, "Xinguan 112"));
set.add (new Person (1, "eson_ho", 22, "E-commerce 112"));
JSONArray jsonArray4 = JSONArray.fromObject (set);
System.out.println ("3.Set collection converted into json code ====>" + jsonArray4.toString ());
4. Convert Bean to json code
JSONObject jsonObject = JSONObject.fromObject (new Person (1, "Beijing 112", 22, "btbu"));
System.out.println ("4.Bean converted to json code ====>" + jsonMap.toString ());
5. Convert the array into json code
boolean [] boolArray = new boolean [] {true, false, true};
String [] stringArray = new String [] {"Xinguan 111", "Xinguan 112", "E-commerce 111", "E-commerce 112"};
JSONArray boolArray1 = JSONArray.fromObject (boolArray);
JSONArray jsonArray2 = JSONArray.fromObject (stringArray);
System.out.println ("5. Array to json code ====>" + boolArray1.toString ());
System.out.println ("5. Array to json code ====>" + jsonArray2.toString ());
6. convert data to json code
JSONArray jsonArray3 = JSONArray.fromObject ("['json', 'is', 'easy']");
System.out.println ("6. General data is converted into json code ====>" + jsonArray3.toString ());
Running result:
1.List collection is converted into json code ====> [{"address": "Haidian", "age": 20, "id": 0, "name": "nagi_ho"}, {"address": " Chaoyang "," age ": 21," id ": 1," name ":" eson_ho "}]
2.Map collection is converted into json code ====> {"arr": ["btbu", "b"], "int": 1, "name": "json", "func": function (i) {return this.arr;}, "bool": true}
3.Set set is converted into json code ====> [{"address": "E-commerce 112", "age": 22, "id": 1, "name": "eson_ho"}, {"address" : "Letter tube 112", "age": 20, "id": 0, "name": "nagi_ho"}]
4.Bean into json code ====> {"arr": ["btbu", "b"], "int": 1, "name": "json", "func": function (i) { return this.arr;}, "bool": true}
5. Array to json code ====> [true, false, true]
5. Array to json code ====> ["Xinguan 111", "Xinguan 112", "E-commerce 111", "E-commerce 112"]
6. General data is converted into json code ====> ["json", "is", "easy"]
Of course, I believe you are not satisfied with the json format, so we use the json formatting tool Hison (click to download)
[
{
"address": "Beijing",
"age": 20,
"id": 0,
"name": "nagi"
},
{
"address": "Beijing",
"age": 21,
"id": 1,
"name": "nag1"
},
{
"address": "Beijing",
"age": 22,
"id": 2,
"name": "nag2"
},
{
"address": "Beijing",
"age": 23,
"id": 3,
"name": "nag3"
},
{
"address": "Beijing",
"age": 24,
"id": 4,
"name": "nag4"
}
]
Is it really nice to have a try,
If you think it is useful to you, give it a compliment ~
Download complete Demo source code