Json类型Mismatch,Android

时间:2014-09-21 09:48:53

标签: java android json

我有一个像这样的JSON。它是一个很长的字符串。我只是粘贴了一些部分,整个部分由http://jsonlint.org/检查。在JSON中没有错误。

    {
    "1": {
        "fajr": "00:0-33",
        "sunrise": "12:10",
        "zuhr": "17:45",
        "asr": "20:48",
        "maghrib": "23:19",
        "isha": "24:52"
    },
    "2": {
        "fajr": "00:0-32",
        "sunrise": "12:09",
        "zuhr": "17:45",
        "asr": "20:49",
        "maghrib": "23:21",
        "isha": "24:53"
    },
    "3": {
        "fajr": "00:0-30",
        "sunrise": "12:07",
        "zuhr": "17:45",
        "asr": "20:50",
        "maghrib": "23:22",
        "isha": "24:54"
    }
}

我像

一样解析它
            HttpRequest request = HttpRequest.get(URL);
            jsonData = request.body();
            System.out.println(jsonData);

            JSONArray jsonArray = new JSONArray(jsonData);

            for (int i = 0; i <= jsonArray.length(); i++) {

                JSONObject jsonObject = jsonArray.getJSONObject(i);
                // GET PRAYER DETAILS
                PrayerTime pt = new PrayerTime();
                pt.setFajr(jsonObject.getString(FAJR));
                pt.setSunrise(jsonObject.getString(SUNRISE));
                pt.setJohr(jsonObject.getString(ZUHR));
                pt.setAsar(jsonObject.getString(ASR));
                pt.setMagrib(jsonObject.getString(MAGRIB));
                pt.setEisha(jsonObject.getString(ISHA));

                Log.e("PrayerTime", "" + pt.getSunrise());

            }`

我有一个像

这样的例外
  09-21 15:47:10.894: W/System.err(16172): org.json.JSONException: Value {"19":{"zuhr":"17:40","asr":"21:03","sunrise":"11:36","fajr":"0-1:0-59","isha":"13:17","maghrib":"23:43"},"17":{"zuhr":"17:41","asr":"21:01","sunrise":"11:40","fajr":"00:0-3","isha":"13:14","maghrib":"23:40"},"18":{"zuhr":"17:41","asr":"21:02","sunrise":"11:38","fajr":"00:0-1","isha":"13:15","maghrib":"23:42"},"15":{"zuhr":"17:42","asr":"21:00","sunrise":"11:43","fajr":"00:0-7","isha":"13:11","maghrib":"23:38"},"16":{"zuhr":"17:41","asr":"21:00","sunrise":"11:41","fajr":"00:0-5","isha":"13:12","maghrib":"23:39"},"13":{"zuhr":"17:42","asr":"20:58","sunrise":"11:47","fajr":"00:0-11","isha":"13:08","maghrib":"23:35"},"14":{"zuhr":"17:42","asr":"20:59","sunrise":"11:45","fajr":"00:0-9","isha":"13:10","maghrib":"23:36"},"11":{"zuhr":"17:43","asr":"20:56","sunrise":"11:51","fajr":"00:0-15","isha":"13:05","maghrib":"23:33"},"12":{"zuhr":"17:42","asr":"20:57","sunrise":"11:49","fajr":"00:0-13","isha":"13:07","maghrib":"23:34"},"21":{"zuhr":"17:40","asr":"21:04","sunrise":"11:32","fajr":"0-1:0-55","isha":"13:20","maghrib":"23:46"},"20":{"zuhr":"17:40","asr":"21:03","sunrise":"11:34","fajr":"0-1:0-57","isha":"13:18","maghrib":"23:44"},"22":{"zuhr":"17:40","asr":"21:05","sunrise":"11:30","fajr":"0-1:0-53","isha":"13:21","maghrib":"23:47"},"23":{"zuhr":"17:39","asr":"21:05","sunrise":"11:28","fajr":"0-1:0-51","isha":"13:22","maghrib":"23:48"},"24":{"zuhr":"17:39","asr":"21:06","sunrise":"11:26","fajr":"0-1:0-49","isha":"13:24","maghrib":"23:49"},"25":{"zuhr":"17:39","asr":"21:07","sunrise":"11:25","fajr":"0-1:0-47","isha":"13:25","maghrib":"23:51"},"26":{"zuhr":"17:38","asr":"21:07","sunrise":"11:23","fajr":"0-1:0-45","isha":"13:27","maghrib":"23:52"},"27":{"zuhr":"17:38","asr":"21:08","sunrise":"11:21","fajr":"0-1:0-42","isha":"13:28","maghrib":"23:53"},"28":{"zuhr":"17:38","asr":"21:09","sunrise":"11:19","fajr":"0-1:0-40","isha":"13:30","maghrib":"23:54"},"29":{"zuhr":"17:37","asr":"21:09","sunrise":"11:17","fajr":"0-1:0-38","isha":"13:31","maghrib":"23:56"},"3":{"zuhr":"17:45","asr":"20:50","sunrise":"12:07","fajr":"00:0-30","isha":"24:54","maghrib":"23:22"},"2":{"zuhr":"17:45","asr":"20:49","sunrise":"12:09","fajr":"00:0-32","isha":"24:53","maghrib":"23:21"},"10":{"zuhr":"17:43","asr":"20:56","sunrise":"11:53","fajr":"00:0-17","isha":"13:04","maghrib":"23:31"},"1":{"zuhr":"17:45","asr":"20:48","sunrise":"12:10","fajr":"00:0-33","isha":"24:52","maghrib":"23:19"},"30":{"zuhr":"17:37","asr":"21:10","sunrise":"11:15","fajr":"0-1:0-36","isha":"13:33","maghrib":"23:57"},"7":{"zuhr":"17:44","asr":"20:53","sunrise":"12:00","fajr":"00:0-23","isha":"13:00","maghrib":"23:27"},"6":{"zuhr":"17:44","asr":"20:52","sunrise":"12:02","fajr":"00:0-24","isha":"24:59","maghrib":"23:26"},"5":{"zuhr":"17:44","asr":"20:51","sunrise":"12:03","fajr":"00:0-26","isha":"24:57","maghrib":"23:25"},"31":{"zuhr":"17:37","asr":"21:11","sunrise":"11:13","fajr":"0-1:0-34","isha":"13:35","maghrib":"23:58"},"4":{"zuhr":"17:44","asr":"20:50","sunrise":"12:05","fajr":"00:0-28","isha":"24:56","maghrib":"23:23"},"9":{"zuhr":"17:43","asr":"20:55","sunrise":"11:54","fajr":"00:0-19","isha":"13:03","maghrib":"23:30"},"8":{"zuhr":"17:43","asr":"20:54","sunrise":"11:56","fajr":"00:0-21","isha":"13:01","maghrib":"23:29"}} of type org.json.JSONObject cannot be converted to JSONArray
09-21 15:47:10.919: W/System.err(16172):    at org.json.JSON.typeMismatch(JSON.java:111)
09-21 15:47:10.919: W/System.err(16172):    at org.json.JSONArray.<init>(JSONArray.java:91)
09-21 15:47:10.919: W/System.err(16172):    at org.json.JSONArray.<init>(JSONArray.java:103)

我错过了什么?

3 个答案:

答案 0 :(得分:2)

在JSON字符串中,有两个符号可指导您完成解析:

{ - 表示JSONObject

[ - 表示JSONArray

虽然您的Json有效,但由于没有&#34; []&#34;,因此无法检测到JSONArray。

要解析此json,请将代码更改为:

JSONObject jsonObject = new JSONObject(jsonData) ;
JSONObject json_one = jsonObject.getJSONObject("1");
JSONObject json_two = jsonObject.getJSONObject("2");
JSONObject json_three = jsonObject.getJSONObject("3");

然后:

            PrayerTime pt = new PrayerTime();
            pt.setFajr(json_one.getString(FAJR));
            pt.setSunrise(json_one.getString(SUNRISE));
            ...

答案 1 :(得分:1)

问题是这个字符串不包含任何JSONArray并且你试图解析json数组

如果没有JSONArray而不是JSONArray jsonArray = new JSONArray(jsonData);这条线是没用的

您可以像这样解决您的问题

Log.i("jsonData", jsonData);
            try{
            JSONObject jobj1=new JSONObject(jsonData);
            for(int i=0;i<31;i++){
                if(jobj1.has(""+i)){
                    JSONObject jobj2=jobj1.getJSONObject(""+i);
                    fazarData.add(jobj2.getString("fajr"));
                }
            }

            Log.i("fajr", ""+fazarData);

            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

答案 2 :(得分:1)

public class YourClass extends Activity {

    public static final String url="";   
    public static final String TAG_1="1";
    public static final String TAG_ASR="asr";
    public static final String TAG_SUNRISE="sunrise";
    public static final String TAG_ISHA="isha";
    public static final String TAG_FAJR="fajr";
    public static final String TAG_ZUHR="zuhr";
    public static final String TAG_MAGHRIB="maghrib";
    public static final String TAG_2="2";
    public static final String TAG_2_OBJ_ASR="asr";
    public static final String TAG_2_OBJ_SUNRISE="sunrise";
    public static final String TAG_2_OBJ_ISHA="isha";
    public static final String TAG_2_OBJ_FAJR="fajr";
    public static final String TAG_2_OBJ_ZUHR="zuhr";
    public static final String TAG_2_OBJ_MAGHRIB="maghrib";
    public static final String TAG_3="3";
    public static final String TAG_3_OBJ_ASR="asr";
    public static final String TAG_3_OBJ_SUNRISE="sunrise";
    public static final String TAG_3_OBJ_ISHA="isha";
    public static final String TAG_3_OBJ_FAJR="fajr";
    public static final String TAG_3_OBJ_ZUHR="zuhr";
    public static final String TAG_3_OBJ_MAGHRIB="maghrib";

    JSONParser jParser = new JSONParser();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    JSONObject json = jParser.getJSONFromUrlByGet(url);
        try {

            JSONObject 1_obj = json.getJSONObject(TAG_1);

                String str_asr = 1_obj.getString(TAG_ASR);

                String str_sunrise = 1_obj.getString(TAG_SUNRISE);

                String str_isha = 1_obj.getString(TAG_ISHA);

                String str_fajr = 1_obj.getString(TAG_FAJR);

                String str_zuhr = 1_obj.getString(TAG_ZUHR);

                String str_maghrib = 1_obj.getString(TAG_MAGHRIB);

            JSONObject 2_obj = json.getJSONObject(TAG_2);

                String str_2_obj_asr = 2_obj.getString(TAG_2_OBJ_ASR);

                String str_2_obj_sunrise = 2_obj.getString(TAG_2_OBJ_SUNRISE);

                String str_2_obj_isha = 2_obj.getString(TAG_2_OBJ_ISHA);

                String str_2_obj_fajr = 2_obj.getString(TAG_2_OBJ_FAJR);

                String str_2_obj_zuhr = 2_obj.getString(TAG_2_OBJ_ZUHR);

                String str_2_obj_maghrib = 2_obj.getString(TAG_2_OBJ_MAGHRIB);

            JSONObject 3_obj = json.getJSONObject(TAG_3);

                String str_3_obj_asr = 3_obj.getString(TAG_3_OBJ_ASR);

                String str_3_obj_sunrise = 3_obj.getString(TAG_3_OBJ_SUNRISE);

                String str_3_obj_isha = 3_obj.getString(TAG_3_OBJ_ISHA);

                String str_3_obj_fajr = 3_obj.getString(TAG_3_OBJ_FAJR);

                String str_3_obj_zuhr = 3_obj.getString(TAG_3_OBJ_ZUHR);

                String str_3_obj_maghrib = 3_obj.getString(TAG_3_OBJ_MAGHRIB);

        } catch (JSONException e){
        }
    }
}