2014-09-12 60 views
0

我有串JSON結構這樣Android的分裂字符串,正則表達式

String response = "{'success':1,'error_code':0,'message':'Access granted'}"

我想分割,使用String.split()是這樣的:

{[success],[1],[error_code],[0],[message],[Access Granted]}

我試過這個solution,this solution too,但沒有一個解決方案符合我的需要。

我該如何做到這一點?

+2

爲什麼要使用String.split()拆分有效的JSON? – 2014-09-12 11:04:31

+0

@SeshuVinay它不是JSON,它的字符串像JSON格式。 – NPE 2014-09-12 11:07:37

+0

如果它是一個JSON字符串,您可以將其視爲JSON字符串。 – Toto 2014-09-12 11:12:09

回答

2

試試這種方式,希望這會幫助你解決你的問題。

try{ 
    JSONObject responseJson = new JSONObject("{\"success\":1,\"error_code\":0,\"message\":\"Access granted\"}"); 
    String valu1 = responseJson.getString("success"); 
    String valu2 = responseJson.getString("error_code"); 
    String valu3 = responseJson.getString("message"); 
}catch (Throwable e){ 
    e.printStackTrace(); 
} 
+0

這個值的逗號怎麼樣:''message「:」Access,granted「' – Toto 2014-09-12 11:07:50

+0

@ M42,這是他要求的那種要求嗎? – 2014-09-12 11:09:35

+0

我已經更新了我的問題,我認爲你的回覆字符串是錯誤的。我需要從'String response =「{'success':1,'error_code':0,'message':'Access granted'}」' – NPE 2014-09-12 11:10:49