2012-07-16 67 views
0

当我试图解析这个有效的JSONJSONException解析一个有效的JSON

我用new JSONObject(resultString);但我有JSONException

如何可以解析的Json他

请帮助我!

由于

[{ 
    "id": "8637F7F78C8C1", 
    "from_account_id": "1025630", 
    "to_account_id": "1025635", 
    "transaction_type_id": "15", 
    "transaction_mode_id": "2", 
    "transaction_status_id": "1", 
    "amount": "1000.00", 
    "real_amount": "1000.00", 
    "promote_amount": "0.00", 
    "from_fix_fee_amount": "0.00", 
    "from_percent_fee_amount": "50.00", 
    "to_fix_fee_amount": "0.00", 
    "to_percent_fee_amount": "0.00", 
    "description": "demo", 
    "created_on": "2012-07-16 10:04:29", 
    "activated_on": "0000-00-00 00:00:00", 
    "executed_on": "0000-00-00 00:00:00", 
    "verify_transaction_by": "o", 
    "from_account_name": "xxxxxxxxxxxx", 
    "from_account_email": "[email protected]", 
    "from_account_phone_no": "xxcxxxxxx", 
    "to_account_name": "yyyyyyy", 
    "to_account_email": "[email protected]", 
    "to_account_phone_no": "yyyyyyyy" 
}, 
{ 
    "id": "A26BF7F75534B", 
    "from_account_id": "1014635", 
    "to_account_id": "1054630", 
    "transaction_type_id": "5", 
    "transaction_mode_id": "2", 
    "transaction_status_id": "4", 
    "amount": "1000.00", 
    "real_amount": "1000.00", 
    "promote_amount": "0.00", 
    "from_fix_fee_amount": "0.00", 
    "from_percent_fee_amount": "0.00", 
    "to_fix_fee_amount": "0.00", 
    "to_percent_fee_amount": "0.00", 
    "description": "", 
    "created_on": "2012-07-15 00:52:40", 
    "activated_on": "2012-07-15 00:53:19", 
    "executed_on": "2012-07-15 00:54:57", 
    "verify_transaction_by": "o", 
    "from_account_name": "yyyyyy", 
    "from_account_email": "[email protected]", 
    "from_account_phone_no": "yyyyyyy", 
    "to_account_name": "wwwwwww", 
    "to_account_email": "[email protected]", 
    "to_account_phone_no": "yyyyyyyyy" 
}] 
+0

07-17 19:48:53.650:W/System.err的(15185):org.json.JSONException:值 “XXXXXX” 07 -17 19:48:53.660:W/System.err(15185):\t at org.json.JSON.typeMismatch(JSON.java:100) – 2012-07-16 13:04:13

+0

共享Stacktrace。这可能会给我们更多的细节。 – 2012-07-16 13:06:39

回答

3

它是一个JSONArray不是JSONObject ..
JSON Array iteration in Android/Java

JSONArray values = new JSONArray(resultString); 
for(int i = 0 ; i < values.length(); i++){ 
    JSONObject object = (JSONObject) values.get(i); 
    String id = object.getString("id"); 
    //the same for the rest 
} 
+0

我如何解析这个JsonArray – 2012-07-16 13:00:43

+0

更新了我的答案 – Nermeen 2012-07-16 13:01:06

+0

这段代码示例工作? – Nermeen 2012-07-16 13:24:56

1

这不是一个JSON对象 - 它是含有两个对象JSON数组。

先使用new JSONArray(resultString);然后从中获取对象。

+0

当我使用:new JSONArray(resultString);我有JSONException – 2012-07-16 13:02:43

1

嗨[]表示数组中的JSON,所以解析尝试后把它放在JSONArray ......以这种方式,你可以得到JSON对象new JSONArray(resultString).getJSONObject(int index);

..

1

为了解析JSON我用GSON

JsonElement jelement = new JsonParser().parse("json text"); 

JsonArray阵列= jobject.getAsJsonArray( “myArray的”);
为 (JsonElement项:阵列){...}