解析JSON字符串,json字符串
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONUtils {
// 讀入JSON字符串並返回List<String>字符串列表
public static List<String> parseCityJSONStr(String str) {
List<String> list = new ArrayList<String>();
JSONObject jsonObject;
try {
// JSON自非常中{}表示JSONObject
// 這裡創建JSONObject對象
jsonObject = new JSONObject(str);
// 根據json對象中冒號前的內容(key)獲取冒號後的內容(values)
// 後面的內容是JSON數組(JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("citys");
// 遍歷JSONArray中的數據
for (int i = 0; i < jsonArray.length(); i++) {
String city = (String) jsonArray.get(i);
list.add(city);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}