2016-09-02 9 views
0

私は、ユーザーオブジェクトを表すJavaで次のHTTP JSON応答を持っています。jsonファイルから子属性を削除する

{ 
    "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" 
} 

は今、私はprojectkey(例えば、「projectPool-pool2は」)の助けを借りて、特定のプロジェクトを検索したいです。その後、要素を完全に削除したい。私の目標はこのプロジェクトなしでHTTPポストコールを送ることです。

結果は私のHTTPポストコールについては、以下のようになります。

{ 
    "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-thirdproject", 
      "projectKey": "projectPool-Pool3", 
      "cqprojectName": "xxxx", 
     } 
    ], 

    "clients": 
    [ 
    ], 
    "dbid": 76864576, 
    "version": 1, 
    "id": "dbpack21" 
} 

まず私は、文字列への応答を解析されてきました。

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

このアプローチは正しいですか?

よろしく!

+0

最後のコードセグメントのStringResponseおよびProjectArrayは、クラスではないオブジェクトであるため、小文字にする必要があります。 – dahui

+0

あなたの早い再生のためにありがとう。それ以外はどうすれば処理できますか? – InfoEngi

+0

小文字の変数は小さな慣習の問題です。質問への私の答えを確認してください – dahui

答えて

0

あなたの配列を持っていたら、何かのようにしてみてください...

// Array to store the indexes of the JSONArray to remove 
ArrayList<Integer> indexesToRemove = new ArrayList<Integer>(); 
// Iterate through projects array, check the object at each position 
// if it contains the string you want, add its index to the removal list 
for (int i = 0; i < projectsArray.length; i++) { 
    JSONObject current = projectsArray.get(i); 
    if (current.get("projectKey") == "**DESIRED PROJECT KEY**") { 
     indexesToRemove.add(i); 
    } 
} 

今、あなたはわからない(メソッドを削除し、JSONArraysで配列から対応するオブジェクトを削除するには、あなたのインデックスを反復処理することができます上記のコードはメモリからのものです)。項目を前に削除してください。削除しないと、以前の項目が削除され、索引が変更され、別の索引を削除すると誤った項目が削除されます。

// Going through the list backwards so we can remove the highest item each 
//time without affecting the lower items 
for (int i = indexesToRemove.size()-1; i>=0; i--) { 
    projectsArray.remove(indexesToRemove.get(i)); 
} 
+0

あなたのリプレイに感謝します エラーが発生しました:org.json.JSONException:JSONObject ["projects" ]は見つかりませんでした "プロジェクト"には独自の属性があるため、正確にはわかりません: -/ – InfoEngi

+0

は現在、多くの感謝です! – InfoEngi

+0

申し訳ありません、私はあなたの2番目の質問JSONを使って作業しているときに、未加工の書式を読みにくい場合はJSONビューアを使用するのがよいでしょう。なぜ追加/取り出し操作がなぜ起こっていないのかがわかりやすくなりますとにかくそれを整理してうれしい! – dahui

関連する問題