2016-03-15 104 views
-4

我得到2,3 & 4個字符串(值)在數組中,但我無法在textview中設置值。我得到的TextView的是隻有4個值也相同repeat.Please告訴我如何處理下面this.JSON:無法從JsonArray獲取字符串

{ 
    results: [ 
    { 
    types: [ 
    "sublocality_level_3", 
    "sublocality", 
    "political" 
    ] 
    }, 
    { 
    types: [ 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    { 
    types: [ 
    "travel_agency", 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    { 
    types: [ 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    { 
    types: [ 
    "store", 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    { 
    types: [ 
    "dentist", 
    "health", 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    { 
    types: [ 
    "clothing_store", 
    "store", 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    { 
    types: [ 
    "point_of_interest", 
    "establishment" 
    ] 
    }, 
    ] 
    } 

碼是什麼我想:

try { 
     object = new JSONObject(url); 
     JSONArray array = object.getJSONArray("results"); 
     for (int i = 0; i < array.length(); i++){ 
      JSONObject object1 = array.getJSONObject(i); 
      JSONArray array1 = object1.getJSONArray("types"); 
      for(int j = 0; j < array1.length(); j++){ 
       if (array1.length()==2) { 
        t1 = array1.getString(0); 
        t2 = array1.getString(1); 
       } if (array1.length()==3) { 
        t1 = array1.getString(0); 
        t2 = array1.getString(1); 
        t3 = array1.getString(2); 
       } if (array1.length()==4) { 
        t1 = array1.getString(0); 
        t2 = array1.getString(1); 
        t3 = array1.getString(2); 
        t4 = array1.getString(3); 
       } 
      } 

      getterSetter.setT1(t1); 
      getterSetter.setT2(t2); 
      getterSetter.setT3(t3); 
      getterSetter.setT4(t4); 
      list.add(getterSetter); 
      adapter = new GooglePlacesAdapter(GooglePlace.this,list); 
      listView.setAdapter(adapter); 
      } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

什麼是你所面臨的問題? – Rohit5k2

+0

我無法得到字符串,它是類型arraylist中的2和3 – AndroidLover

+0

重新編輯您的問題。您的JSON文件無效。 [請首先閱讀此鏈接](http://www.json.org/) – Bonatti

回答

0

你應該有類似這樣

GetterSetter getterSetter = new GetterSetter(); 

移動此行的第一個for循環內的一行。

注意:絕對不需要inner for loop(2nd)。你可以刪除那個循環,只保留if語句。

更新:您的代碼應該是這樣

try { 
    object = new JSONObject(url); 
    JSONArray array = object.getJSONArray("results"); 
    for (int i = 0; i < array.length(); i++){ 
     JSONObject object1 = array.getJSONObject(i); 
     JSONArray array1 = object1.getJSONArray("types"); 
     GetterSetter getterSetter = new GetterSetter(); // This line should be inside for loop 
     if (array1.length()==2) { 
      getterSetter.setT1(array1.getString(0)); 
      getterSetter.setT2(array1.getString(1)); 
     } else if (array1.length()==3) { 
      getterSetter.setT1(array1.getString(0)); 
      getterSetter.setT2(array1.getString(1)); 
      getterSetter.setT3(array1.getString(2)); 
     } else if (array1.length()==4) { 
      getterSetter.setT1(array1.getString(0)); 
      getterSetter.setT2(array1.getString(1)); 
      getterSetter.setT3(array1.getString(2)); 
      getterSetter.setT4(array1.getString(3)); 
     } 

     list.add(getterSetter); 
     adapter = new GooglePlacesAdapter(GooglePlace.this,list); 
     listView.setAdapter(adapter); 
    } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 
+0

有2個字符串的數組不響應意味着我沒有獲得值 – AndroidLover

+0

rohit!在這裏有幾個領域。當我得到50-60場時如何處理。 – AndroidLover

+0

你的'GetterSetter'類應該使用'ArrayList'而不是單個字符串。這樣,你可以簡單地繼續向該數組列表添加字符串,並最終在循環中根據需要採取行動。 – Rohit5k2

-1

第一陣列應該是在格式不在語法

{ 
"results": [ 
{ 
    "types": [ 
    "sublocality_level_3", 
"sublocality", 
"political" 
] 
}, 
{ 
"types": [ 
"point_of_interest", 
"establishment" 
] 
}, 
{ 
"types": [ 
"travel_agency", 
"point_of_interest", 
"establishment" 
] 
}, 
{ 
"types": [ 
"point_of_interest", 
"establishment" 
] 
}, 
{ 
"types": [ 
"store", 
"point_of_interest", 
"establishment" 
] 
}, 
{ 
"types": [ 
"dentist", 
"health", 
"point_of_interest", 
"establishment" 
] 
}, 
{ 
"types": [ 
"clothing_store", 
"store", 
"point_of_interest", 
"establishment" 
] 
}, 
{ 
"types": [ 
"point_of_interest", 
"establishment" 
] 
} 
] 
} 

在代碼中你這樣做

try { 
    object = new JSONObject(url); 
    JSONArray array = object.getJSONArray("results"); 
    for (int i = 0; i < array.length(); i++){ 
     JSONObject object1 = array.getJSONObject(i); 
     JSONArray array1 = object1.getJSONArray("types"); 
     for(int j = 0; j < array1.length(); j++){ 
      if (array1.length()==2) { 
       t1 = array1.getString(0); 
       t2 = array1.getString(1); 
       t3 = ""; 
       t4 = ""; 
      } else if (array1.length()==3) { 
       t1 = array1.getString(0); 
       t2 = array1.getString(1); 
       t3 = array1.getString(2); 
       t3 = ""; 
       t4 = ""; 
      } else if (array1.length()==4) { 
       t1 = array1.getString(0); 
       t2 = array1.getString(1); 
       t3 = array1.getString(2); 
       t4 = array1.getString(3); 

      } 
      getterSetter.setT1(t1); 
      getterSetter.setT2(t2); 
      getterSetter.setT3(t3); 
      getterSetter.setT4(t4); 
      list.add(getterSetter); 
     } 

     adapter = new GooglePlacesAdapter(GooglePlace.this,list); 
     listView.setAdapter(adapter); 
     } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 

希望這有助於您