JSONObject user_values = new JSONObject(json.getString("quotes_cat"));
JSONArray user_subject = new JSONArray(
json.getString("quotes_cat"));
List<String> list = new ArrayList<String>();
for (int i = 0; i < user_subject.length(); i++)
{
list.add(user_subject.getString(i));
}
String[] stringArray = list.toArray(new String[list.size()]);
问题是
类型org.json.JSONArray无法转换为JSONObject
JSON输出就是这个
{"status":"ok","response":"Fetched Successfully!","quotes_cat":["Life","Love","Success","Happiness","Funny","Love","Success","Happiness","Funny"]}
答案 0 :(得分:2)
替换此
JSONObject user_values = new JSONObject(
json.getString("quotes_cat"));
用这个:
JSONObject user_values = new JSONObject(json);
您需要以这种方式将原始JSON字符串解析为JSONObject Java对象。之后,替换为:
JSONArray user_subject = new JSONArray(
json.getString("quotes_cat"));
用这个:
JSONArray user_subject = user_values.getJSONArray("quotes_cat"));
从新创建的根JSONObject中获取数组: - )
答案 1 :(得分:0)
你的quotes_cat
是jsonArray,所以试试这个:
JSONArray user_subject = new JSONObject(json).getJSONArray("quotes_cat");
List<String> list = new ArrayList<String>();
for (int i = 0; i < user_subject.length(); i++)
{
list.add(user_subject.getString(i));
}
String[] stringArray = list.toArray(new String[list.size()]);
当json
是您的json字符串时:
String json = "{\"status\":\"ok\",\"response\":\"Fetched Successfully!\",\"quotes_cat\":[\"Life\",\"Love\",\"Success\",\"Happiness\",\"Funny\",\"Love\",\"Success\",\"Happiness\",\"Funny\"]}";
答案 2 :(得分:0)
假设json
类是JSONObject
,我建议您使用getJSONArray("quotescat")
来检索您的数组。