【问题标题】:org.json.JSONException: Value at of type java.lang.String cannot be converted to JSONArrayorg.json.JSONException:java.lang.String 类型的值无法转换为 JSONArray
【发布时间】:2016-05-07 11:04:11
【问题描述】:

我收到了这个错误

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();
        }
    }

请建议我解决这个问题的任何想法。

【问题讨论】:

    标签: android json


    【解决方案1】:

    替换这两个语句:

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

    用那句话:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 2015-11-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多