Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从结构上修改JsonObject并替换它的一些值?_Java_Android_Json_Gson - Fatal编程技术网

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标记。请指定如何添加标记的值。