2013-05-21 32 views
0

沒有數組名JSON對象我J​​SON這樣得到Android的2

array (
    'status' => 'OK', 
    'categories' => 
    array (
    'Sepeda' => 
    array (
     83 => 'Sepeda MTB', 
     'Fullbike' => 
     array (
     370 => 'MTB', 
     371 => 'Roadbike', 
     374 => 'Fixie', 
     375 => 'Sepeda Lipat', 
     378 => 'City Bike', 
     380 => 'BMX', 
     382 => 'Onthel', 
    ), 

,我想問問,怎麼代碼來獲得「Fullbike」?感謝您的建議,真的需要幫助,感謝

+3

這不是json。這是PHP。 – 323go

+0

你的意思是在json_encode之後?你的意思是你想讓「Fullbike」使用位置?或不使用「Fullbike」獲得Json數組? –

回答

0

結帳的PHP文件,讓您的數組JSON:PHP:json_encode

或者,可以解析PHP數組,因爲它是,但聽起來不像樂趣。

第二次結帳Android JSON handling docs以瞭解如何使用Android處理JSON。這是最好的JSONObject

做下面是一個例子

 
String getJsonThingy(String rawJson) { 
String strFullBike = ""; 
     try { 
      JSONObject json = new JSONObject(rawJson); 
      JSONObject jsonCategories = json.getJSONObject("categories"); 
      JSONObject jsonSepeda = jsonCategories.getJSONObject("Sepeda"); 
      strFullBike = jsonSepeda.getString("Fullbike"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return strFullBike; 

我想指出的是,這個代碼是不是你希望你的最終產品是什麼樣子 - 它是一個開始。

此外,請查看這些相關的SO問題,以幫助您更好地瞭解您應該查找的內容。

最後一點:它可能不是一個壞主意,你的Android客戶端來問你爲你的陣列的特定部分服務器,參觀瞭解您的數組是嵌套的。

0
try { 
    //get Full Bike Json object first 
    JSONObject jsonObject = response.getJSONObject("categories").getJSONObject("Sepeda").getJSONObject("Fullbike"); 
    //Get inner values like this now 
     String mtb = jsonObject.getString("370"); 
     Log.e("Response", mtb);   
} catch (JSONException e) { 
     e.printStackTrace(); 
}