2013-07-22 33 views
0

錯誤解析數據org.json.JSONException:什麼是錯誤表示Android中(JSON)錯誤解析數據org.json.JSONException:

java.lang.String類型的值不能轉化爲JSONArray

JSONArray jsonArray = new JSONArray(result); 
        JSONArray ja= (JSONArray) jsonArray.get(0); 
        JSONObject jb = (JSONObject) ja.get(0); 
        //String firstvalue = jb.getString("0"); 
        int secondvalue = jb.getInt("customer_id"); 

Myjson輸出謹[[{"0":"2","customer_id":"2"}]]

我正在jenerating使用PHP輸出像

$output[]= $customerid; 
print(json_encode($output)); 
+1

在這裏發表您的JSON。 – Raghunandan

+0

@Raghunandan我試過你的代碼,說上述錯誤。 – Vini

+0

在SO本身發佈您的問題。 – Raghunandan

回答

1

要你分析問題的已經回答@

How to call the JSON object in Android

它是一個字符串

String secondvalue = jb.getString("CUSTOMER_ID"); 
try 
{ 
    int value = Integer.parseInt("secondvalue"); 
    Log.i("Customer id",""+value); 
}catch(NumberFormatException e) 
{ 
    e.printStackTrace(); 
} 

編輯:

String myjson = "[" 
        +"[" 
        +"{" 
        + " \"0\": \"2\"," 
        + "\"CUSTOMER_ID\": \"2\"" 
        + "}" 
        + "]" 
        + "]"; 

解析

try { 
     JSONArray jsonArray = new JSONArray(myjson); 
     JSONArray ja= (JSONArray) jsonArray.get(0); 
     JSONObject jb = (JSONObject) ja.get(0); 
     String firstvalue = jb.getString("0"); 
     String secondvalue = jb.getString("CUSTOMER_ID"); 
     int value = Integer.parseInt(secondvalue); 
     Log.i("Customer id",""+value); 
     Log.i("first value is",firstvalue); 
     Log.i("second value is",secondvalue); 
    }catch(NumberFormatException e) 
    { 
    e.printStackTrace(); 
    }catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

Thankyou你的答案解決了我的問題。 – Vini

+0

@ user2607115歡迎您接受以前的答案也因爲這也解決了您的問題。 – Raghunandan

0

它只是意味着作爲輸入給new JSONArray(some_string)的字符串(比如some_string)不能轉換爲JSON數組,因爲該字符串不遵循所需的json數組格式。適當的JSON陣列串的例子在下面給出

[{ 「標題」: 「TITLE_1」, 「名稱」: 「NAME_1」},{ 「標題」: 「TITLE_2」, 「命名 「:」 NAME_2" }]