Jon Java Object Decision Introduction: Understand the framework in the Java class library
Jon Java Object Decision Introduction: Understand the framework in the Java class library
Overview:
The Java object representation (Jon) is a lightweight data exchange format for representative data.It is similar to XML and JSON, but compared to these two formats, Jon is easier to read, write and analyze.Jon is a framework in the Java library, which supports data in Jon format in Java applications.
Jon basic structure:
JON uses key value pairs to represent data.Each key value is composed of a key (key) and a value, and is separated by a colon (:) in the middle.Between the key value pairs, the comma (,) is separated by a comma.The values in JON can be simple data types, such as integer, floating point numbers, string, etc., can also be complex data types, such as lists, mapping, etc.
Example of JON:
The following is an example of using Jon to represent student information:
{
"name": "Zhang San",
"age": 20,
"Gender": "Man",
"grades": [85, 90, 92, 88],
"address": {
"City": "Beijing",
"Street": "Chaoyang District",
"zipcode": "100001"
}
}
In the above example, student information includes names, age, gender, grades and addresses.The name, age, and gender value are simple data types, the results are a list, and the address is a mapping.
Use JON framework to process Jon data:
The Jon framework in the Java class library provides a set of classes and methods to process and operate data in Jon format.The following is a simple sample code. Demonstrate how to read and analyze the student information in the above example:
import com.company.Student;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
// Define the string in the jon format
String jsonstr = "{\" name \ ": \" Zhang San \ ", \" Age \ ": 20," Gender \ ": \" Men \ "," +
"\" Grades \ ": [85, 90, 92, 88], \" address \ ": {\" city \ ": \" Beijing \ ", \" street \ ": \" Chaoyang District \ ", \ \"Zipcode \": \ "100001 \"}} ";
// Analysis Jon data
JSONObject jsonObj = new JSONObject(jsonStr);
// Read student information from the Jon object
String name = jsonObj.getString("name");
int age = jsonObj.getInt("age");
String gender = jsonObj.getString("gender");
int[] grades = jsonObj.getJSONArray("grades").toList().stream().mapToInt(obj -> (int) obj).toArray();
JSONObject addressObj = jsonObj.getJSONObject("address");
String city = addressObj.getString("city");
String street = addressObj.getString("street");
String zipcode = addressObj.getString("zipcode");
// Create a student object
Student student = new Student(name, age, gender, grades, city, street, zipcode);
// Output the name and address of the student object
System.out.println ("Student Name:" + Student.getName ());
System.out.println ("Student Address:" + Student.getAddress ());
}
}
In the above example, we first define a jon format string, and then use the JSONObject class in the Jon frame to analyze it as a Jon object.Next, we read student information from the Jon object and used this information to create a student object.Finally, we output the name and address of the student object.
Summarize:
By using the Jon framework, we can easily process and operate data in Jon format.It provides a simple and flexible way to represent and exchange structured data.Whether it is transmitted data in network communication or stored and processing data in Java applications, Jon is a very useful tool.I hope this article can help you understand Jon's basic concepts and usage methods.