https://blog.csdn.net/u011575570/article/details/47863337
When the socket transmits data in Java, the data type is often difficult to choose. Issues such as bandwidth, cross-language, and version compatibility may be considered. There are two common methods: one is to wrap the object into a JSON string for transmission, and the other is to use the serialization and deserialization of java objects. With the open source of Google's tool protoBuf, protobuf is also a good choice. Compare JSON, Object Serialize, and ProtoBuf.
1, string to json
There are three ways
The first: string directly to json
String json = "{\"2\":\"efg\",\"1\":\"abc\"}"; JSONObject json_test = JSONObject.fromObject(json); Escape the double quotes of the string , for shorter strings
The second: convert string to list and then convert to json
List<String> list = new ArrayList<String>(); list.add("username"); list.add("age"); list.add("sex"); JSONArray array = new JSONArray(); array.add(list);
You can use the add function of list to splicing the required strings, but this can only use jsonarry
The third type: convert string to map and then convert to json
Map<String, String> map = new HashMap<String, String>();
map.put("1", "abc");
map.put("2", "efg");
JSONArray array_test = new JSONArray();
array_test.add(map);
JSONObject jsonObject = JSONObject.fromObject(map);
You can use map here to convert strings to JSONArray or JSONObject , but the keys here cannot use int type
1. json to string
先构造json:JSONObject string_to_json = JSONObject.fromObject("{\"data\": {\"pages\": [ {\"comment\": \"just for test\"},{\"comment\": \"just for test\"}],\"total_count\": 2 },\"errcode\": 0}");
For JSONObject , it can be used directly
JSONObject json_to_data = string_to_json.getJSONObject("data");即可
For JSONArray , you can use these two
The first: JSONArray json_to_strings = json_to_data.getJSONArray("pages");//First take out the JSONArray contained in the JSONObject
for (Object object : json_to_strings) {//The loop can be read
JSONObject json_to_string = JSONObject.fromObject(object);
json_to_string.get("pages");
}
The second: JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");//First take out the JSONArray contained in the JSONObject
for (int i = 0; i < json_to_strings_test.size(); i++) {//It can be read in a loop
JSONObject json_to_string1 = json_to_strings_test.getJSONObject(i);
}
The test of all the above programs is shown in the figure:
Paste the code below:
//string constructs json
String json = "{\"2\":\"efg\",\"1\":\"abc\"}";
JSONObject json_test = JSONObject.fromObject(json);
System.out.print("json_test:"+json_test);
System.out.print("\n");
//Use list to construct json (list can only use JSONArray)
List<String> list = new ArrayList<String>();
list.add("username");
list.add("age");
list.add("sex");
JSONArray array = new JSONArray();
array.add(list);
System.out.print("array:"+array);
System.out.print("\n");
//Initialize the HashMap collection and add an array (json must not be an int key)
Map<String, String> map = new HashMap<String, String>();
map.put ("1", "abc");
map.put("2", "efg");
JSONArray array_test = new JSONArray();
array_test.add(map);
System.out.print("array_test:"+array_test);
System.out.print("\n");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.print("jsonObject:"+jsonObject);
System.out.print("\n");
//parse json
JSONObject string_to_json = JSONObject.fromObject("{\"data\": {\"pages\": [ {\"comment\": \"just for test1\"},{\"comment\": \"just for test2\"}],\"total_count\": 2 },\"errcode\": 0}");
JSONObject json_to_data = string_to_json.getJSONObject("data");
JSONArray json_to_strings = json_to_data.getJSONArray("pages");
for (Object object : json_to_strings) {
JSONObject json_to_string = JSONObject.fromObject(object);
json_to_string.get("pages");
System.out.print("json_to_string.get(\"pages\"):"+json_to_string.get("comment"));
System.out.print("\n");
}
JSONObject json_to_data1 = string_to_json.getJSONObject("data");
JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");
for (int i = 0; i < json_to_strings_test.size(); i++) {
JSONObject json_to_string1 = json_to_strings_test.getJSONObject(i);
System.out.print("json_to_string1.get(\"pages\"):"+json_to_string1.get("comment"));
System.out.print("\n");
}
There are new and good methods I hope to discuss
JSONObject.fromObject--JSON and object conversion
There is such a sentence in the code, which is the package data in the background.
Convert common java code to json
--Please note that this method used to confuse me. Because, when it converts Object, it converts according to all getXXX() methods in the domain class. If you write a non-attribute getXXX() method in the class, then the XXX attribute will be returned to you.
--Use the classes under net.sf.json.* (jar package is json-lib-xxjar)
1. Convert the List collection to json code
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Convert the Map collection to json code
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
3. Bean is converted into json code
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. Convert array to json code
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );