Import java. util. arraylist;
Import java. util. iterator;
Import java. util. linkedhashmap;
Import java. util. List;
Import java. util. Map;
/**
* JSON Utility Class
*
* @ Since 2008-04-21
*/
Public class JSON {
// Test
Public static void main (string [] ARGs ){
JSON json1 = new JSON ();
Json1.add ("totalcount", 2 );
Json1.add ("istest", true );
JSON json_a = new JSON ();
Json_a.add ("menuid", 1 );
Json_a.add ("menuname", "testmenu ");
Json1.add ("topics", json_a );
JSON json_ B = new JSON ();
Json_ B .add ("menuid", 2 );
Json_ B .add ("menuname", "testmenu ");
Json1.add ("topics", json_ B );
System. Out. println (json1.tostring ());
}
Private map = new linkedhashmap ();
/**
* Add a JSON property with a value of a string. An array is generated when repeated values are added. <p/>
*
* Add ("name", "value"); <br/>
* Add a string, and the generated JSON is: {"name": "value"} <p/>
*
* Add ("name", "value1"); <br/>
* Add ("name", "value2"); <br/>
* Add two strings with the same attribute. The generated JSON is {"name": ["value1", "value2"]} <p/>
*
* @ Param key JSON attribute name
* @ Param STR string format Attribute Value
*/
Public void add (string key, string value ){
Addelement (Key, value );
}
Public void add (string key, int num ){
Addelement (Key, new INTEGER (Num ));
}
Public void add (string key, Boolean B ){
Addelement (Key, new Boolean (B ));
}
/**
* Add a JSON attribute with a value of JSON. a json array is generated when repeated values are added. <p/>
*
* JSON json1 = new JSON (); <br/>
* Json1.add ("name1", "value1"); <br/>
* Json1.add ("name2", "value2"); <br/>
* JSON = new JSON (); <br/>
* JSON. Add ("message", json1); <br/>
* Add a JSON file. The generated JSON file is {"message": {"name1": "value1", "name2": "value2" }}< P/>
*
* JSON json1 = new JSON (); <br/>
* Json1.add ("name1", "value1"); <br/>
* Json1.add ("name2", "value2"); <br/>
* JSON json2 = new JSON (); <br/>
* Json2.add ("name1", "value3"); <br/>
* Json2.add ("name2", "value4"); <br/>
* JSON = new JSON (); <br/>
* JSON. Add ("message", json1); <br/>
* JSON. Add ("message", json2); <br/>
* Add two JSON files with the same attribute. The generated JSON files include: {"message": [{"name1": "value1", "name2": "value2 "}, {"name1": "value3", "name2": "value4"}]} <p/>
*
* @ Param key JSON attribute name
* @ Param JSON: attribute value in JSON format
*/
Public void add (string key, JSON ){
Addelement (Key, JSON );
}
Public String tostring (){
Stringbuilder sb = new stringbuilder ();
SB. append ("{");
Int K = 0;
For (iterator I = map. keyset (). iterator (); I. hasnext ();){
String key = (string) (I. Next ());
Object OBJ = map. Get (key );
If (k> 0 ){
SB. append (",");
}
Appendkey (SB, key );
If (OBJ instanceof string ){
Appendstring (SB, (string) OBJ );
} Else if (OBJ instanceof list ){
Appendlist (SB, (list) OBJ );
} Else if (OBJ instanceof JSON ){
Appendjson (SB, (JSON) OBJ );
} Else {
Appendother (SB, OBJ );
}
K ++;
}
SB. append ("}");
Return sb. tostring ();
}
Private void addelement (string key, object OBJ ){
If (! Map. containskey (key )){
If (OBJ instanceof JSON ){
List list = new arraylist ();
List. Add (OBJ );
Map. Put (Key, list );
} Else {
Map. Put (Key, OBJ );
}
Return;
}
Object o = map. Remove (key );
If (O instanceof list ){
(List) O). Add (OBJ );
Map. Put (Key, O );
Return;
}
// O is a string
List list = new arraylist ();
List. Add (O );
List. Add (OBJ );
Map. Put (Key, list );
}
/**
* Append JSON property name
*
* @ Param sb
* @ Param key
*/
Private void appendkey (stringbuilder Sb, string key ){
SB. append ("/" "). append (key). append ("/":");
}
/**
* Append JSON property value that is a string
*
* @ Param sb
* @ Param Str
*/
Private void appendstring (stringbuilder Sb, string Str ){
SB. append ("/" "). append (STR). append ("/"");
}
/**
* Append JSON property value that is a integer
*
* @ Param sb
* @ Param num
*/
Private void appendother (stringbuilder Sb, object OBJ ){
SB. append (OBJ );
}
/**
* Append JSON property value that is a list
*
* @ Param sb
* @ Param list
*/
Private void appendlist (stringbuilder Sb, list ){
SB. append ("[");
For (Int J = 0, M = List. Size (); j <m; j ++ ){
If (j> 0 ){
SB. append (",");
}
Object OBJ = list. Get (j );
If (OBJ instanceof string ){
Appendstring (SB, (string) OBJ );
} Else if (OBJ instanceof JSON ){
Appendjson (SB, (JSON) OBJ );
} Else {
Appendother (SB, OBJ );
}
}
SB. append ("]");
}
/**
* Append JSON property value that is a JSON
*
* @ Param sb
* @ Param JSON
*/
Private void appendjson (stringbuilder Sb, JSON ){
SB. append (JSON. tostring ());
}
}