好吧,所以我试图从JSON的列表中提取数据,例如:
{"code":200,"status":"OK","data":{"timings":{"Fajr":"04:17","Sunrise":"05:30","Dhuhr":"12:20","Asr":"15:42","Sunset":"19:11","Maghrib":"19:11","Isha":"20:24","Imsak":"04:07","Midnight":"00:20"},"date":{"readable":"17 Jun 2017","timestamp":"1497652236"}}}
专门的祈祷。我找到数据的代码如下:
class SaveTheFeed extends AsyncTask<Void, Void, Void>{
String jsonString = "";
String result = "";
EditText chosenCity = (EditText) findViewById(R.id.editText3);
String city = chosenCity.getText().toString();
@Override
protected Void doInBackground(Void... voids) {
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost("http://api.aladhan.com/timingsByCity?city=" + city + "&country=AE&method=2");
httpPost.setHeader("Content-type", "application/json");
InputStream inputStream;
try{
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) !=null){
builder.append(line + "\n");
}
jsonString = builder.toString();
JSONObject jObject = new JSONObject(jsonString);
JSONObject data = (JSONObject) jObject.get("data");
JSONArray jArray = (JSONArray) data.get("timings");
outputTimings(jArray);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
TextView prayerTimes = (TextView) findViewById(R.id.prayerTimes);
prayerTimes.setText(result);
}
protected void outputTimings(JSONArray jsonArray){
String[] prayers= {"Fajr", "Sunrise", "Dhuhr", "Asr", "Sunset", "Maghrib", "Isha", "Imsak", "Midnight"};
try{
for(int i = 0; i < jsonArray.length(); i++){
JSONObject cityObject =
jsonArray.getJSONObject(i);
result = result + prayers[i] + " : "+
cityObject.getString(prayers[i]) + "\n";
}
我收到指向第85行的错误(JSONObject jObject = new JSONObject(jsonString);行)
06-17 00:24:15.973 2866-2907 / azanmute.android.com.azanmute W / System.err:org.json.JSONException:类型java.lang.String的值无法转换为JSONObject
06-17 00:24:15.973 2866-2907 / azanmute.android.com.azanmute W / System.err:at org.json.JSON.typeMismatch(JSON.java:111)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:位于org.json.JSONObject。(JSONObject.java:160)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:位于org.json.JSONObject。(JSONObject.java:173)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at azanmute.android.com.azanmute.MainActivity $ SaveTheFeed.doInBackground(MainActivity.java:85)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at azanmute.android.com.azanmute.MainActivity $ SaveTheFeed.doInBackground(MainActivity.java:60)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at android.os.AsyncTask $ 2.call(AsyncTask.java:305)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)
06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at java.lang.Thread.run(Thread.java:761)
所以我的理解是,我错误地指向数据或将JSONObjects与JSONArrays混合在一起。香港专业教育学院已经尝试过this和this,但仍然收到相同的错误。
最佳答案
在这种情况下,您无需混淆JSON对象和JSON数组。这绝对是JSONObject。
您可能已在JSONObject构造函数不喜欢的字符串中添加了“不可见”字符(即“ \ n”)。尝试这个:
JSONObject jObject = new JSONObject(jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}") + 1));
还请注意,“时间”也是JSONObject,而不是JSONArray。 JSONArray的示例,其中“ cars”是JSONArray:
{"name":"John","age":30,"cars":[ "Ford", "BMW", "Fiat" ]}