org.json.JSONException:类型为java.lang.String的值无法转换为JSONArray

时间:2016-05-07 11:04:11

标签: android json

我收到此错误

org.json.JSONException: Value [{"value":"6","startDate":"1970-01-01T00:00:00","measureTypeId":1,"endDate":"1970-01-01T00:00:00","isTimeSet":true},{"value":"12","startDate":"1970-01-01T00:00:00","measureTypeId":1,"endDate":"1970-01-01T00:00:00","isTimeSet":true}] at measureTooteets of type java.lang.String cannot be converted to JSONArray
执行以下代码时

if (tweetType == TooteetType.MEASURE) {
            measureJson = cursor.getString(cursor.getColumnIndex(MEASURE_JSON));
            Log.d("ss","Measure json frm database______________"+measureJson);
            if (measureJson != null) {
                try {
                    JSONObject object = new JSONObject(measureJson);
                 //   JSONArray jsonArray = object.getJSONArray("measureTooteets");
                 //   new JSONObject("{your string}")
                    String notes = object.getString("measureTooteets");
                    JSONArray jsonArray  = new JSONArray(notes);
                    mMeasure = new Measure(jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

其中Measure(jsonArray)将访问以下代码

public Measure(JSONArray jsonArray) {
        try {
//            JSONArray jsonArray = object.getJSONArray("MeasureTooteets");
            for(int i =0; i<jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                id = jsonObject.optString("id");
                tooteetId = jsonObject.optString("tooteetId");
                laneId = jsonObject.optString("laneId");
                startDate = jsonObject.optString("startDate");
                endDate = jsonObject.optString("endDate");
                value = jsonObject.optDouble("value");
                measureTypeId = jsonObject.optInt("measureTypeId");
                isTimeSet = jsonObject.optBoolean("isTimeSet");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

请建议我解决这个问题。

2 个答案:

答案 0 :(得分:0)

替换这两个陈述:

 String notes = object.getString("measureTooteets");
 JSONArray jsonArray  = new JSONArray(notes);

声明:

 JSONArray jsonArray  = object.getJSONArray("measureTooteets");

答案 1 :(得分:0)

您的代码是正确的,但问题可能出在 measureJson 的内容中,请尝试给我一个示例,说明此对象中存在的内容,以帮助我检测问题