2012-11-06 66 views
1

解析我有這樣的Json以下哪我從.NET Web服務越來越:的Json不關鍵,Android的

[["Developer","test","[email protected]"]] 

我jsonlint驗證檢查,這表明上述JSON罰款。

因爲它沒有任何key我不知道如何解析它在Android中。 有人可以幫助我如何解析這個沒有密鑰的json。

如果有鑰匙,我會嘗試這樣做:

{"first":"somevalue","last":"someothervalue"} // Json response 

JSONObject json_obj = new JSONObject(resp); //response is the json array 
String first = json_obj.getString("first"); 
String last = json_obj.getString("last"); 
String test = "Hello! " + first + " " + last; 

希望有人幫助:)

問候,

普利文

我的解決方案

JSONArray respo = new JSONArray(resp); //resp is Json Array 
respo = respo.getJSONArray(0); 
String test = respo.getString(2); 
test = "Your Email Address is " + test; 
+0

它看起來像你的JSON是一個數組中的數組,所以第一個元素實際上來到包含整個數據集。我不熟悉Android如何處理這些,但但可以稍後檢查併發布答案(電話atm) –

+0

那麼,如何在不使用'key'的情況下解析它? –

+0

這不是響應中的JSON數組,它是一個String數組數組。 – thepoosh

回答

7

(Java)的解析這一行看起來像:

String jsonString="[[\"Developer\",\"test\",\"[email protected]\"]]"; 
try { 
    JSONArray jsonArray=new JSONArray(jsonString); 
    JSONArray jsonArray2=jsonArray.getJSONArray(0); 

    for (int i = 0; i < jsonArray2.length(); i++) { 
    String valueString=jsonArray2.getString(i); 
    Log.e("json", i+"="+valueString); 
    } 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

這是如此有幫助:) – playmaker420

2

使用JSONArray而不是JSONObject。然後使用getJSONArray()函數獲取數組。之後,您可以使用索引和getString()訪問所有值。

Android中
+0

隨着你的想法,我建立了這個:)現在工作:) –