Java Object goto JSON object

Source: Internet
Author: User
Tags goto

Org.java class

public class Orgs {    private String ID;    private String name;    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;    }}

User.java class

public class Users {    private String ID;    private String name;    private String password;    Private String address;    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 String GetPassword () {        return password;    }    public void SetPassword (String password) {        this.password = password;    }    Public String getaddress () {        return address;    }    public void setaddress (String address) {        this.address = address;    }    }

Test class

Import Net.sf.json.jsonarray;import Net.sf.json.jsonobject;public class Test {/** * * @param args */P        Ublic static void Main (string[] args) {Users users=new users ();        Users.setid ("1111");        Users.setname ("Zhang San");        Users.setpassword ("888888");                Users.setaddress ("xxx province");        Users u1=new users ();        U1.setid ("2222");        U1.setname ("John Doe");        U1.setpassword ("888888");                U1.setaddress ("xxx province");        Orgs orgs=new orgs ();        Orgs.setid ("1111");                Orgs.setname ("xxx Province xxx unit");        Jsonobject obj=new jsonobject ();        Jsonarray userarr=new Jsonarray ();                Jsonarray orgarr=new Jsonarray ();        Jsonobject JSON = jsonobject.fromobject (users);        Jsonobject Json1 = Jsonobject.fromobject (U1);        Jsonobject Orgjson = Jsonobject.fromobject (orgs);        Userarr.add (Json.tostring ());        Userarr.add (Json1.tostring ());               Orgarr.add (Orgjson.tostring ()); OBj.put ("Users", userarr.tostring ());        Obj.put ("Orgs", orgarr.tostring ());    System.out.println (Obj.tostring ()); }}

Run results

{"    users": [        {            "address": "xxx Province",            "id": "1111", "            name": "Zhang San",            "password": "888888"        },        {            "address": "xxx Province",            "id": "2222",            "name": "John Doe",            "password": "888888"        }    ],    " Orgs ": [        {            " id ":" 1111 ",            " name ":" xxx Province xxx unit "        }    ]}

  

Java Object goto JSON object

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.