SoFunction
Updated on 2025-03-08

Example of implementation method for converting java objects and json objects

This article describes the implementation method of converting Java objects and json objects. Share it for your reference, as follows:

import ;
import ;
import ;
import ;
import ;
import ;
public class MainClass {
  public static void main(String[] args) {
    TestJsonBean();
    TestJsonAttribute();
    TestJsonArray();
  }
  @SuppressWarnings("rawtypes")
  private static void TestJsonArray() {
    Student student1 = new Student();
    (1);
    ("jag");
    ("man");
    (25);
    (new String[]{"basketball","game"});
    Student student2 = new Student();
    (2);
    ("tom");
    ("woman");
    (23);
    (new String[]{"On the Internet","running"});
    List<Student> list = new ArrayList<Student>();
    (student1);
    (student2);
    JSONArray jsonArray = (list);
    (());
    JSONArray new_jsonArray=(());
    Collection java_collection=(new_jsonArray);
    if(java_collection!=null && !java_collection.isEmpty())
    {
      Iterator it=java_collection.iterator();
      while(())
      {
        JSONObject jsonObj=(());
        Student stu=(Student) (jsonObj,);
        (());
      }
    }
  }
  private static void TestJsonAttribute() {
    /**
      * Create a json object and set properties for the object
      */
    JSONObject jsonObj = new JSONObject();
    ("Int_att",25);//Add int-type attribute    ("String_att","str");//Add string attribute    ("Double_att",12.25);//Add double attribute    ("Boolean_att",true);//Add boolean attribute    //Add JSONObject type attribute    JSONObject jsonObjSon = new JSONObject();
    ("id", 1);
    ("name", "tom");
    ("JSONObject_att",jsonObjSon);
    //Add JSONArray type attribute    JSONArray jsonArray = new JSONArray();
    ("array0");
    ("array1");
    ("array2");
    ("array3");
    ("JSONArray_att", jsonArray);
    (());
    ("Int_att:"+("Int_att"));
    ("String_att:"+("String_att"));
    ("Double_att:"+("Double_att"));
    ("Boolean_att:"+("Boolean_att"));
    ("JSONObject_att:"+("JSONObject_att"));
    ("JSONArray_att:"+("JSONArray_att"));
  }
  /**
    * Java objects and json objects convert each other
    */
  private static void TestJsonBean() {
    /**
      * Create java object
      */
    Student student = new Student();
    (1);
    ("jag");
    ("man");
    (25);
    (new String[]{"basketball","On the Internet","running","game"});
    /**
      * Convert java object into json object and get json object properties
      */
    JSONObject jsonStu = (student);
    (());
    (("hobby"));
    /**
      * Convert json object into java object and get java object properties
      */
    Student stu = (Student) (jsonStu, );
    (());
    /**
      * Create a json object
      */
    JSONObject jsonObj = new JSONObject();
    ("id",1);
    ("name","Zhang Yong");
    ("sex","male");
    ("age",24);
    //("hobby",new String[]{"online","game","running","music"});    //(());
    /**
      * Convert json object to java object
      */
    Student stud = (Student) (jsonObj,);
    (());
  }
}

PS: Regarding json operation, here are some more practical json online tools for your reference:

OnlineJSONCode verification, inspection, beautification and formatting tools:
http://tools./code/json

JSONOnline formatting tools:
http://tools./code/jsonformat

Online XML/JSONConvert each other tools:
http://tools./code/xmljson

jsonCode online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

OnlinejsonCompression/escaping tools:
http://tools./code/json_yasuo_trans

For more information about Java related content, please check out the topic of this site:Summary of Java operation json format data skills》、《Summary of Java array operation skills》、《Summary of Java characters and string operation techniques》、《Summary of Java mathematical operation skills》、《Java Data Structure and Algorithm Tutorial"and"Summary of Java operating DOM node skills

I hope this article will be helpful to everyone's Java programming.