JSONException:尝试从JSON

时间:2017-06-16 22:34:54

标签: java android json

好吧,我试图从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); line)

  

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:at org.json.JSONObject。(JSONObject.java:160)   06-17 00:24:15.974 2866-2907 / azanmute.android.com.azanmute W / System.err:at 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混合在一起。我已经尝试了thisthis但仍然收到同样的错误。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您没有混淆JSON Object和JSON Array。这绝对是一个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" ]}