Java 如何从结构上修改JsonObject并替换它的一些值?
我在Java 如何从结构上修改JsonObject并替换它的一些值?,java,android,json,gson,Java,Android,Json,Gson,我在JsonObject中有一个json,例如: "customer": { "full_name": "John John", "personal": { "is_active": "1", "identifier_info": { "hobbies": [ "skiing", "traveling"
JsonObject
中有一个json,例如:
"customer": {
"full_name": "John John",
"personal": {
"is_active": "1",
"identifier_info": {
"hobbies": [
"skiing",
"traveling"
],
"user_id": "1234",
"office_id": "2345"
}
}
}
是否有办法修改JsonObject
,以便将嗜好
从标识符信息
中完全删除,其余内容保持不变,然后以与其他内容相同的方式添加嗜好
?i、 e
"customer": {
"full_name": "John John",
"personal": {
"is_active": "1",
"identifier_info": {
"skiing":"expert",
"traveling":"rarely",
"user_id": "1234",
"office_id": "2345"
}
}
}
使用
JsonObject
的方法应该没有问题。它返回删除的jsonement
。假设原始json是名为customer
的JsonObject:
JsonObject identifierInfo = customer.getAsJsonObject("personal").getAsJsonObject("identifier_info");
JsonArray hobbies = (JsonArray) identifierInfo.remove("hobbies");
之后,您只需将爱好添加到标识信息
,即可获得所需的结果:
for (JsonElement aHobby : hobbies) {
identifierInfo.addProperty(aHobby.getAsString(), "expert");
}
不要忘记根据需要添加空检查。使用
JsonObject
的方法,这应该没有问题。它返回删除的jsonement
。假设原始json是名为customer
的JsonObject:
JsonObject identifierInfo = customer.getAsJsonObject("personal").getAsJsonObject("identifier_info");
JsonArray hobbies = (JsonArray) identifierInfo.remove("hobbies");
之后,您只需将爱好添加到标识信息
,即可获得所需的结果:
for (JsonElement aHobby : hobbies) {
identifierInfo.addProperty(aHobby.getAsString(), "expert");
}
不要忘记根据需要添加空检查。找到删除JSON数组“嗜好”的完整实现,并将它们直接插入父JSON对象中
public class ProcessJSONString {
String data ="{\"customer\": { \n" +
" \"full_name\": \"John John\", \n" +
" \"personal\": { \n" +
" \"is_active\": \"1\", \n" +
" \"identifier_info\": { \n" +
" \"hobbies\": [ \n" +
" \"skiing\",\n" +
" \"traveling\"\n" +
" ], \n" +
" \"user_id\": \"1234\", \n" +
" \"office_id\": \"2345\"\n" +
" } \n" +
" } \n" +
"}} ";
public void processData() {
try {
JSONObject result = new JSONObject(data);
JSONObject customer = result.getJSONObject("customer");
JSONObject personal = customer.getJSONObject("personal");
JSONObject identifierInfo =
personal.getJSONObject("identifier_info");
JSONArray hobbies = identifierInfo.getJSONArray("hobbies");
identifierInfo.remove("hobbies");
//Under the assumption the tags will be added by the user, the code has been inserted.
identifierInfo.put("skiing","expert");
identifierInfo.put("traveling","rarely");
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ProcessJSONString instance = new ProcessJSONString();
instance.processData();
}
}
找到删除JSON数组“嗜好”的完整实现,并将它们直接插入父JSON对象中
public class ProcessJSONString {
String data ="{\"customer\": { \n" +
" \"full_name\": \"John John\", \n" +
" \"personal\": { \n" +
" \"is_active\": \"1\", \n" +
" \"identifier_info\": { \n" +
" \"hobbies\": [ \n" +
" \"skiing\",\n" +
" \"traveling\"\n" +
" ], \n" +
" \"user_id\": \"1234\", \n" +
" \"office_id\": \"2345\"\n" +
" } \n" +
" } \n" +
"}} ";
public void processData() {
try {
JSONObject result = new JSONObject(data);
JSONObject customer = result.getJSONObject("customer");
JSONObject personal = customer.getJSONObject("personal");
JSONObject identifierInfo =
personal.getJSONObject("identifier_info");
JSONArray hobbies = identifierInfo.getJSONArray("hobbies");
identifierInfo.remove("hobbies");
//Under the assumption the tags will be added by the user, the code has been inserted.
identifierInfo.put("skiing","expert");
identifierInfo.put("traveling","rarely");
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ProcessJSONString instance = new ProcessJSONString();
instance.processData();
}
}
在服务器端执行此操作。如果你愿意,可以在没有爱好的情况下得到回应。这些爱好的价值从何而来,这一点很容易弄清楚。另外,这个问题不是特定于android的,所以应该使用Java标记。请指定如何添加标记的值。在服务器端执行此操作。如果你愿意,可以在没有爱好的情况下得到回应。这些爱好的价值从何而来,这一点很容易弄清楚。另外,这个问题不是特定于android的,所以应该使用Java标记。请指定如何添加标记的值。