Code example for Gson

Methods: toJson

0
        Map<String, Object> subMap = new LinkedHashMap<String, Object>();
        subMap.put("subKeyA", "valueA");
        map.put("key2", subMap);
        map.put("key3", "value3");
 
        Gson gson = new Gson();
 
        // Serialize to JSON 
        String json = gson.toJson(map);
        Logger.info("Serialized to: %s", json);
        // Results in {"key1":"value1","key2":{"subKeyA":"valueA"},"key3":"value3"} 
        assertThat(json, containsString("\"key2\":{\"subKeyA\":\"valueA\"}"));
    } 
 
} 
Experience pair programming with AI  Get Codota for Java