1. If we need to implement a configuration management function, adding a field for each configuration item is both complex and not conducive to expansion. Therefore, we usually use a string to save the configuration item information, this section describes how to use JSON string parsing to achieve the purpose just described. Introduce the class library required by JSON:
Import org. JSON. jsonexception;
Import org. JSON. jsonobject;
2. Generate a JSON object (you can add different types of data ):
Jsonobject = new jsonobject ();
Jsonobject. Put ("A", 1); jsonobject. Put ("B", 1.1 );
Jsonobject. Put ("C", 1l );
Jsonobject. Put ("D", "test ");
Jsonobject. Put ("e", true );
System. Out. println (jsonobject );
// {"D": "test", "E": True, "B": 1.1, "C": 1, "A": 1}
3. parse a JSON object (different types of data can be parsed), getjsonobject (string Str ):
Jsonobject = getjsonobject ("{d: Test, E: True, B: 1.1, C: 1, A: 1 }");
System. Out. println (jsonobject );
// {"D": "test", "E": True, "B": 1.1, "C": 1, "A": 1}
System. Out. println (jsonobject. getint (""));
System. Out. println (jsonobject. getdouble ("B "));
System. Out. println (jsonobject. getlong ("C "));
System. Out. println (jsonobject. getstring ("D "));
System. Out. println (jsonobject. getboolean ("e "));
4,
Public static jsonobject getjsonobject (string Str ){
If (STR = NULL | Str. Trim (). Length () = 0 ){
Return NULL;
}
Jsonobject = NULL;
Try {
Jsonobject = new jsonobject (STR );
} Catch (jsonexception e ){
E. printstacktrace (system. Err );
}
Return jsonobject;
}
Pack: http://www.json.org/java/index.html