异常:JSONException: End of input at character 0

Quibbler 4月前 315

异常:JSONException: End of input at character 0


        解析json字符串时出现org.json.JSONException: End of input at character 0 of 异常

    val jsonObject = JSONObject(response)
    jsonObject.getString("data");


        原因是传入参数为""空的字符串,或者为"    "n个空格,回到Json对象的构造方法中:

    /**
     * Creates a new {@code JSONObject} with name/value mappings from the JSON
     * string.
     *
     * @param json a JSON-encoded string containing an object.
     * @throws JSONException if the parse fails or doesn't yield a {@code
     *     JSONObject}.
     */
    public JSONObject(@NonNull String json) throws JSONException {
        this(new JSONTokener(json));
    }

        借助JSONTokener去解析json格式字符串:

    /**
     * Creates a new {@code JSONObject} with name/value mappings from the next
     * object in the tokener.
     *
     * @param readFrom a tokener whose nextValue() method will yield a
     *     {@code JSONObject}.
     * @throws JSONException if the parse fails or doesn't yield a
     *     {@code JSONObject}.
     */
    public JSONObject(@NonNull JSONTokener readFrom) throws JSONException {
        /*
         * Getting the parser to populate this could get tricky. Instead, just
         * parse to temporary JSONObject and then steal the data from that.
         */
        Object object = readFrom.nextValue();
        if (object instanceof JSONObject) {
            this.nameValuePairs = ((JSONObject) object).nameValuePairs;
        } else {
            throw JSON.typeMismatch(object, "JSONObject");
        }
    }

         其中nextValue()JSONTokener中读取解析Json:

    /**
     * Returns the next value from the input.
     *
     * @return a {@link JSONObject}, {@link JSONArray}, String, Boolean,
     *     Integer, Long, Double or {@link JSONObject#NULL}.
     * @throws JSONException if the input is malformed.
     */
    public Object nextValue() throws JSONException {
        int c = nextCleanInternal();
        switch (c) {
            case -1:
                throw syntaxError("End of input");
            case '{':
                return readObject();
            case '[':
                return readArray();
            case '\'':
            case '"':
                return nextString((char) c);
            default:
                pos--;
                return readLiteral();
        }
    }

        syntaxError(String message)方法抛出异常:

    /**
     * Returns an exception containing the given message plus the current
     * position and the entire input string.
     */
    public JSONException syntaxError(String message) {
        return new JSONException(message + this);
    }

        JSONTokener注意到的toString()方法

    /**
     * Returns the current position and the entire input string.
     */
    @Override public String toString() {
        // consistent with the original implementation
        return " at character " + pos + " of " + in;
    }

        最后,整个异常message就组成了End of input at character 0 。


       处理也很简单,确保jsonString是一个有效的、非空的JSON字符串。如果是从外部源获取数据,检查数据流或文件是否正确打开和读取。确保jsonString是一个有效的、非空的JSON字符串。

    if (response.isNotBlank()) {
        val jsonObject = JSONObject(response)
    }

        如果是从外部源获取数据,检查数据流或文件是否正确打开和读取。


不忘初心的阿甘
最新回复 (0)
    • 安卓笔记本
      2
        登录 注册 QQ
返回
仅供学习交流,切勿用于商业用途。如有错误欢迎指出:fluent0418@gmail.com