Convert string and json to each other in java

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) {//You can read it in a loop      JSONObject json_to_string = JSONObject.fromObject(object) ;      json_to_string.get("pages");     } 



The second type: JSONArray json_to_strings_test = json_to_data1.getJSONArray("pages");//First take out the JSONArray contained in JSONObject     for (int i = 0; i < json_to_strings_test.size(); i++) {//loop read 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 have keys that cannot be int types)
        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");
    //解析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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326381282&siteId=291194637