Conversion between a Java object and a JSON object

Source: Internet
Author: User
Tags object object string to json

Jar Package:import net.sf.json.JSONObject;

1. Parsing JSON strings

Convert the JSON string to a JSON object, and then parse the JSON object:.
Jsonobjectjsonobject = Jsonobject.fromobject (JSONSTR);
Gets its value based on the key in the JSON
Stringname = jsonobject.getstring ("name");
int age = Jsonobject.getint ("Age");

2. Converting a JSON string to a Java object
The JSON string is also converted to a JSON object before the JSON object is converted to a Java object, as shown below.
Jsonobjectobj = new Jsonobject (). Fromobject (JSONSTR);//Convert a JSON string to a JSON object
To convert a JSON object to a Java object
PERSONJB = (person) jsonobject.tobean (obj,person.class);//convert built JSON object to person object

3. Converting a Java object to a JSON string

Converts a Java object to a JSON object before converting a JSON object to a JSON string
Jsonobjectjson = Jsonobject.fromobject (obj);//Convert Java object to JSON object
Stringstr = Json.tostring ();//Convert a JSON object to a string

Instance code:

  1. Package Test3;
  2. Public class Person {
  3. private int age;
  4. public int getage () {
  5. return age;
  6. }
  7. public void Setage (int.) {
  8. this.age = age;
  9. }
  10. Public String GetName () {
  11. return name;
  12. }
  13. public void SetName (String name) {
  14. this.name = name;
  15. }
  16. private String name;
  17. Public String toString () {
  18. return "person [age=" + Age + ", name=" + name + "]";
  19. }
  20. Public person (int age, String name) {
  21. super ();
  22. this.age = age;
  23. this.name = name;
  24. }
  25. Public person (String personstr) {
  26. }
  27. Public Person () {
  28. }
  29. }

  1. Package Test3;
  2. Import Net.sf.json.JSONObject;
  3. Public class Jsontoobject {
  4. /** 
  5. * @param args
  6. */
  7. public Void Test () {
  8. System.out.println ("Convert Object object, string to JSON object");
  9. Person person = New Person (1, "CGF");
  10. Jsonobject json = jsonobject.fromobject (person);
  11. SYSTEM.OUT.PRINTLN (JSON);
  12. System.out.println ("Turn the string into a JSON object and then to a custom object");
  13. String jsonstring = "{" + "age" + ": 1" + " ," + "name" + ":" + " ' wbj '" + "}";
  14. Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
  15. System.out.println (Jsonobject);
  16. Person Person1 = (person) jsonobject.tobean (jsonobject, person.   Class);
  17. System.out.println ("Name:" +person1.getage () + "Age:" + person1.getname ());
  18. System.out.println ("Convert Java object to JSON string");
  19. Jsonobject Json1 = jsonobject.fromobject (person);
  20. String jsonString1 = json1.tostring ();
  21. System.out.println (JSONSTRING1);
  22. }
  23. public static void Main (string[] args) {
  24. //TODO auto-generated method stub
  25. Jsontoobject JSON = new Jsontoobject ();
  26. Json.test ();
  27. }
  28. }

Conversion between a Java object and a JSON object

Related Article

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.