2016-11-21 87 views
-5

我想從下面的json文件中刪除json值。從Json中刪除Json值

這是我的JSON對象:

{ 
    "Approximation": { 
    "name": "Approximation", 
    "description": [ 
     "Approximation", 
     "Approximation", 
     "Approximation" 
    ], 
    "video": [ 
     "evideo/package1/maths/Approximation/349188_f2ba28819a74355a456ef91d95451b71/349188_f2ba28819a74355a456ef91d95451b71/349188.mpd", 
     "evideo/package3/maths/approxomation/396183_Approximation/396183_fcedf516e0f5c935d561d486058fa6e0/396183.mpd", 
     "evideo/package2/maths/approxomation/387010_01approximation/387010_949440b1082fea19faa7dcb4aebf0b43/387010.mpd" 
    ] 
    } 
} 

,這就是我想要

代碼:

for (int i = 0; i < jsonArr.length(); i++) { 

      JSONObject jsonObj = jsonArr.getJSONObject(i); 
      jsonArr.remove(i); 
      children = new ArrayList<ChildrenEvent>(); 
      et = new Events(jsonObj.optString("name")); 
      ct = new ChildrenEvent(jsonObj.optString("description")); 
      langs.add(et); 
      children.add(ct); 
    } 
+1

所以最新你的問題? – Fay007

+0

我想刪除單詞「Approximation」:從json並保存在應用程序的列表數組中 –

+0

下哪個鍵? 「JSON」中出現了很多「近似」。 –

回答

0

中有一個JSONObject的方法,即是remove()。它將幫助你從jsonObject中刪除任何鍵;

使用此: -

jsonObject.remove("key"); 
1

我不知道你正試圖在這裏實現什麼。您不需要刪除關鍵字「Approximation」來獲取數據。

這就是你如何解析這個JSON

{ 
    "Approximation": { 
     "name": "Approximation", 
     "description": ["Approximation", "Approximation", "Approximation"], 
     "video": ["evideo/package1/maths/Approximation/349188_f2ba28819a74355a456ef91d95451b71/349188_f2ba28819a74355a456ef91d95451b71/349188.mpd", "evideo/package3/maths/approxomation/396183_Approximation/396183_fcedf516e0f5c935d561d486058fa6e0/396183.mpd", "evideo/package2/maths/approxomation/387010_01approximation/387010_949440b1082fea19faa7dcb4aebf0b43/387010.mpd"] 
    } 
} 

代碼,

JSONObject mainObj = new JSONObject(json_string); 
JSONObject appproxObj = mainObj.getJSONObject("Approximation"); 

// Parse name 
String name = appproxObj.getString("name"); 
et = new Events(name); 
langs.add(et); 

// Parse descriptions 
JSONArray descriptions = appproxObj.getJSONArray("description"); 
children = new ArrayList<ChildrenEvent>(); 
for (int i = 0; i < descriptions.length(); i++) { 
    String description = descriptions.getString(i); 
    ct = new ChildrenEvent(description); 
    children.add(ct);  
} 

不過如果你需要JSON無鍵 「逼近」,變量appproxObjJSON沒有密鑰。