【问题标题】:How do i get, modify and post a json object?我如何获取、修改和发布 json 对象?
【发布时间】:2016-09-05 13:16:04
【问题描述】:

正如我 3 天前的旧主题中提到的 - Last Topic

我收到一个 json 响应并将其更改为字符串。 Json 响应代表一个用户对象。在用户对象中,我想搜索一个特定的项目并将其删除。之后,我想通过 HttpPost 再次发布。

    private static String getContent(HttpResponse response) {
HttpEntity entity = response.getEntity();
if (entity == null) return null;
BufferedReader reader;
try {
    reader = new BufferedReader(new InputStreamReader(entity.getContent()));
    String line = reader.readLine();
    reader.close();
    return line;
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return null;

}

String StringResponse = getContent(JsonResponse);
JSONObject jsonObject = new JSONObject(StringResponse);
JSONArray ProjectsArray= jsonObject.getJSONArray("projects");

通过将属性保存在 JsonArray 中来搜索特定项目。

ArrayList<Integer> indexesToRemove = new ArrayList<Integer>();
for (int i = 0; i < projectsArray.length; i++) {
JSONObject current = projectsArray.get(i);
if (current.get("projectKey") == "**ProjectName**") {
indexesToRemove.add(i);
}
} 

正在删除项目...

for (int i = indexesToRemove.size()-1; i>=0; i--) 
{ 
projectsArray.remove(indexesToRemove.get(i));
}

效果很好,我搜索的项目已被删除。但问题是,我想通过 HttpPost 再次发布修改后的 UserObject/String。我删除的项目只是在我的 JsonArray“projectsArray”中,而不是从一开始就在我的字符串中。我不能发布“projectsArray”....

        HttpPost UserChange = new HttpPost (TestUserURL+user);  //TODO: 
        UserChange.setHeader("Accept", "application/json");
        UserChange.setHeader("Content-type", "application/json");

        params = new StringEntity("ModifiedJsonString", HTTP.UTF_8);   // How do i get the complete Json string?
        UserChange.setEntity(params);

        HttpResponse UserChangeResponse = httpclient.execute(UserChange);

        HttpEntity entity2 = UserChangeResponse.getEntity();
            if (entity2 != null) {
                entity2.consumeContent();                       
            }

我需要“ModifiedJsonString”,它从头开始包含完整的 json 文件。

params = new StringEntity(ModifiedJsonString, HTTP.UTF_8);

最好的问候

【问题讨论】:

    标签: java json http-post http-get


    【解决方案1】:

    以下代码删除选定项目之一。

    String jsonString = "{    \"account\": \"Kpatrick\",    \"firstname\": \"Patrick\",    \"instances\": [        {            \"id\": \"packerer-pool\",            \"key\": \"packerer-pool123\",            \"userAccount\": \"kpatrick\",            \"firstname\": \"Patrick\",            \"lastname\": \"Schmidt\"        }    ],    \"projects\": [        {            \"id\": \"packerer-projectPool\",            \"projectKey\": \"projectPool-Pool\",            \"cqprojectName\": \"xxxxx\"        },        {            \"id\": \"packerer-secondproject\",            \"projectKey\": \"projectPool-Pool2\",            \"cqprojectName\": \"xxxx\"        },        {            \"id\": \"packerer-thirdproject\",            \"projectKey\": \"projectPool-Pool3\",            \"cqprojectName\": \"xxxx\"        }    ],    \"clients\": [],    \"dbid\": 76864576,    \"version\": 1,    \"id\": \"dbpack21\"}";
    JSONParser parser = new JSONParser();
    JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
    
    ArrayList<String> listOfNodes = new ArrayList<String>();
    JSONArray projectArray = (JSONArray) jsonObject.get("projects");
    int len = projectArray.size();
    if (projectArray != null) {
        for (int i = 0; i < len; i++) {
            String projectId = ((JSONObject) projectArray.get(i)).get("projectKey").toString();
            if (!projectId.equals("projectPool-Pool2")) {
                listOfNodes.add(projectArray.get(i).toString());
    
            }
    
        }
    }
    // Remove the element from arraylist
    // Recreate JSON Array
    JSONArray jsArray = new JSONArray();
    jsArray.addAll(listOfNodes);
    jsonObject.remove(projectArray);
    jsonObject.put("projects", listOfNodes);
    
    System.out.println(jsonObject.toString());
    

    例如,打印以下 JSON 字符串,删除其中一个项目。 一旦你有了 this ,你就可以使用它来创建一个 StringEntity ,然后在 HTTPPost 调用中使用它。希望对你有帮助

    【讨论】:

    • 太棒了!感谢您的回复。解决方法是:jsonObject.put("projects", listOfNodes);
    猜你喜欢
    • 2020-04-25
    • 2017-02-22
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多