2016-09-05 4 views
0

wie in meinem älteren Thema vor 3 Tagen erwähnt - Last TopicWie bekomme, modifiziere und poste ich ein JSON-Objekt?

Ich habe eine JSON-Antwort und änderte es in eine Zeichenfolge. Die Json-Antwort repräsentiert ein Benutzerobjekt. Innerhalb des User-Objekts wollte ich nach einem bestimmten Projekt suchen und es löschen. Danach möchte ich es über HttpPost erneut posten.

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"); 

für ein bestimmtes Projekt Suchen nach den Attributen in einem JsonArray speichern.

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); 
} 
} 

Löschen des Projekts ...

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

, die gelöscht perfekt und mein gesucht Projekt arbeitet. Aber das Problem ist, dass ich das geänderte UserObject/String erneut über HttpPost veröffentlichen möchte. Und mein gelöschtes Projekt ist nur in meinem JsonArray "projectsArray" und nicht in meinem String von Anfang an. Ich kann nicht schreiben „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();      
      } 

ich das „ModifiedJsonString“ benötigen, die die vollständige JSON-Datei von Anfang an enthält.

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

Mit freundlichen Grüßen

Antwort

0

Der folgende Code entfernt eine des ausgewählten Projekts.

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()); 

Dies druckt beispielsweise die folgende JSON-Zeichenfolge, die eines der Projekte entfernt. Linted JSON Sobald Sie dies haben, können Sie dies verwenden, um eine StringEntity zu erstellen und sie dann in HTTPPost-Aufrufen zu verwenden. Hoffe es hilft

+0

Großartig! Danke für deine Antwort. Die Auflösung war folgender Punkt: jsonObject.put ("projects", listOfNodes); – InfoEngi

Verwandte Themen