2017-02-25 70 views
0

我從MySQL數據庫json字符串:怎麼辦JSON字符串轉換爲字符串

{"teams":[{"sport":"NFL"},{"sport":"NBA"},{"sport":"NCAA Football"},{"sport":"NCAA Men Basketball"}],"success":1} 

,我需要這個字符串轉換爲以下格式:

final String sports[] = {"NFL", "NBA", "NCAA Football", "NCAA Men Basketball"}; 

任何想法如何做這個?

+3

這是什麼語言? – mindOfAi

+0

大量的想法......並不多的線索 – grail

+0

一個技巧是使用這樣的正則表達式:「sport」:「([A-Za-z \ s] *)」捕獲組恰恰是體育詞 –

回答

1
import org.json.*; 
String jsonString = yourJsonStringFromDatabase; 
JSONObject obj = new JSONObject(jsonString); 

JSONArray team = obj.getJSONArray("teams"); 
String[] sports = new String[team.length()); 
for (int i = 0; i < team.length(); i++) 
{ 
    sports[i] = arr.getJSONObject(i).getString("sport"); 
}