2016-09-05 9 views
0

3日前 - Last Topicjsonオブジェクトを取得、変更、および投稿するにはどうすればよいですか?

私はjson応答を得てそれを文字列に変更しました。 Json Responseはユーザーオブジェクトを表します。 User-Object内では、私は特定のプロジェクトを検索して削除したがっていました。その後、私は再びHttpPost経由で投稿したいと思います。 JsonArrayの属性を保存することで、特定のプロジェクトの検索

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

。プロジェクトを削除

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

。しかし、問題は、私は変更されたUserObject/StringをHttpPost経由で再度投稿したいということです。そして私の削除したプロジェクトは私のJsonArray "projectsArray"の中にあり、最初から私の文字列にはありません。私は最初から完全なJSONファイルを含む「ModifiedJsonString」を、必要とする....

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

を「projectsArray」を投稿することはできません。

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

よろしく

答えて

0

次のコードは、選択したプロジェクトの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文字列を出力してプロジェクトの1つを削除します。 Linted JSON これを取得したら、これを使用してStringEntityを作成し、HTTPPost呼び出しで使用できます。希望することができます

+0

素晴らしい!お返事をありがとうございます。解決策はこの時点でした: jsonObject.put( "projects"、listOfNodes); – InfoEngi

関連する問題