Translating Java basic data into JSON code

Source: Internet
Author: User

    JSON是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。是移动开发中数据客户端和服务器端数据交互的不二选择;  尤其是现在HTML5的流行,更显着使用Json 完成数据交互的重要性.      在这里, 我将演示一下Java基本数据转换为Json代码

JSON Required Package

    1. Json-lib-2.3-jdk15.jar
    2. Commons-lang-2.4.jar
    3. Commons-logging-1.0.4.jar
    4. Ezmorph-1.0.6.jar
    5. Commons-collections-3.2.1.jar
    6. Commons-beanutils-1.8.0.jar

If you don't want to download the jar yourself, you can use this demo

1.List Collection converted to JSON code

        Listlistnew ArrayList<Person>();        list.add(new Person(0,"nagi_ho"20,"海淀"));        list.add(new Person(1"eson_ho"21,"朝阳"));         JSONArray jsonList = JSONArray.fromObject(list);         System.out.println("1.List集合转换成json代码====>" + jsonList.toString());

2.Map Collection converted to JSON code

MapMap=NewHashMap ();Map. put ("Name","JSON");Map. put ("BOOL", boolean.true);Map. put ("int",NewInteger(1));Map. put ("Arr",NewString[] {"BTBU","B"});Map. put ("Func","function (i) {return this.arr;}"); Jsonobject Jsonmap = Jsonobject.fromobject (Map); System.out.println("2.Map Collection converted to JSON code ====>"+jsonmap.tostring ());

3.Set Collection converted to JSON code

          Set<Person> Set = new hashset<person> ();          set. Add (New person (0, "Nagi_ho", "Xin Guan"));          set. Add (New person (1, "Eson_ho", "e-commerce"));Jsonarray JsonArray4 = Jsonarray.fromobject ( set);System.out.println ("3.the set set is converted to JSON code ====>"+jsonarray4.tostring ()); 

4.Bean Convert to JSON code

         JSONObject jsonObject = JSONObject.fromObject(new Person(1"信管112"22,"btbu"));         System.out.println("4.Bean转换成json代码====>"+jsonMap.toString());

5. Convert arrays into JSON code

boolean[] Boolarray = new boolean[] {true, False, true};string[] Stringarray = new string[] {"Xin Guan 111","Xin Guan","E-commerce 111","e-commerce"};Jsonarray boolArray1 = Jsonarray. Fromobject(Boolarray);Jsonarray jsonArray2 = Jsonarray. Fromobject(Stringarray);System. out. println("5. Convert arrays into JSON code ====>"+boolarray1. toString());System. out. println("5. Convert arrays into JSON code ====>"+jsonarray2. toString());

6. Conversion of general data into JSON code

         JSONArray jsonArray3 = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]" );         System.out.println("6.一般数据转换成json代码====>"+jsonArray3.toString());

Operation Result:

1.ListCollection into JSON code ====>[{"Address":"Haidian","Age": -,"id":0,"Name":"Nagi_ho"},{"Address":"Sunrise","Age": +,"id":1,"Name":"Eson_ho"}]2.Map collection into JSON code ====>{"Arr":["BTBU","B"],"int":1,"Name":"JSON","Func": function(i){ returnThis.arr; },"BOOL":true}3.The set collection is converted into JSON code ====>[{"Address":"e-commerce","Age": A,"id":1,"Name":"Eson_ho"},{"Address":"Xin Guan","Age": -,"id":0,"Name":"Nagi_ho"}]4.The bean is converted to JSON code ====>{"Arr":["BTBU","B"],"int":1,"Name":"JSON","Func": function(i){ returnThis.arr; },"BOOL":true}5.Arrays into JSON code ====>[true,false,true]5.Arrays into JSON code ====>["Xin Guan 111","Xin Guan","E-commerce 111","e-commerce"]6.Convert general data to JSON code ====>["JSON","is","Easy"]

Of course I believe you are not satisfied with this JSON format, so we use the JSON Format tool Hison (click to download)

[    {        "Address":"Beijing",        " Age": -,        "ID":0,        "name":"Nagi"    },    {        "Address":"Beijing",        " Age": +,        "ID":1,        "name":"Nag1"    },    {        "Address":"Beijing",        " Age": A,        "ID":2,        "name":"Nag2"    },    {        "Address":"Beijing",        " Age": at,        "ID":3,        "name":"Nag3"    },    {        "Address":"Beijing",        " Age": -,        "ID":4,        "name":"Nag4"    }]

Does that look good, try it,

If you feel that it is useful for you to praise it ~

Demo full source download

Translating Java basic data into JSON code

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.