3天前在我的旧话题中提到 - Last Topic
我得到了一个json响应并将其更改为字符串。 Json Response表示用户对象。在User-Object中,我想搜索特定项目并将其删除。之后,我想通过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);
最好的问候
答案 0 :(得分:0)
以下代码会删除所选项目之一。
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字符串,删除其中一个项目。 完成后,您可以使用它来创建StringEntity,然后在HTTPPost调用中使用它。希望它有所帮助