String jsonString = "{\"Apple\":{"
+ "\"cost\":\"0\"},"
+ "\"Pen\":{"
+ "\"cost\":\"0\"}}";
I want to do the following two points.
" 0 "
." Apple Pen "
.This time, I will use the following library for operating typical (?) Json.
Android Studio: 3.4.1 gradle: 5.1.1
Describe the library to be added in the dependencies block of build.gradle.
dependencies {
...
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
implementation 'org.glassfish:javax.json:1.1.4'
}
Gson
Create Json string with the created method (editJson)
Replaced with Map <String, MAP <String, String >>
(Gson.fromJson) and assigned the desired key and value.
After the substitution, the Map is converted back to a Json string (Gson.toJson).
Although it deviates from the main subject, Convert [Json => other class] to "Deserialization", The conversion of [other class => Json] is called "Serialization".
import com.google.gson.Gson;
...
public class GsonExample {
String jsonString = "{\"Apple\":{" +
"\"cost\":\"0\"}," +
"\"Pen\":{" +
"\"cost\":\"0\"}}";
public void main() {
editJson("Apple", "100");
/*
{
"Apple": {
"cost": "100"
},
"Pen": {"
"cost": "0"
}
}"
*/
editJson("ApplePen", "");
/*
{
"Apple": {
"cost": "100"
},
"Pen": {"
"cost": "0"
}
"ApplePen": {"
"cost": "0"
}
}"
*/
}
private void editJson(String key, String str) {
if (str.isEmpty()) str = "0";
Map<String, String> innerMap = new HashMap<>();
innerMap.put("cost", str);
Gson gson = new Gson();
//Convert Json string to Map
Map<String, Map<String,String>> jsonMap = gson.fromJson(jsonString, Map.class);
jsonMap.put(key, innerMap);
//Convert Map to Json string again
jsonString = gson.toJson(jsonMap);
}
}
Jackson The replacement process is performed in almost the same way as for Gson. Jackson translates Json via the ʻObjectMapper` method.
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
...
public class JacksonExample {
// main()Is omitted
private void editJson(String str, String key) {
if (str.isEmpty()) str = "0";
Map<String, Object> map = null;
Map<String, String> innerMap = new HashMap<>();
innerMap.put("cost", str);
ObjectMapper mapper = new ObjectMapper();
try {
//Convert Json string to Map
map = mapper.readValue(json,
new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
e.printStackTrace();
}
map.put(key, innerMap);
try {
//Convert Map to Json string again
json = mapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
I tried to implement the same with the javax.json library, but I got stuck.
I realized that if you just want to convert Json to Map and add a value, you can use ʻorg.json.JSONObject`.
import org.json.JSONException;
import org.json.JSONObject;
...
public class JSONExample {
// main()Is omitted
private void editJson(String str, String key) {
if (str.isEmpty()) str = "0";
Map<String, String> innerMap = new HashMap<>();
innerMap.put("cost", str);
try {
JSONObject jsonObject = new JSONObject(json);
jsonObject.put(key, new JSONObject(innerMap));
json = jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
I referred to the following article.
Recommended Posts