android - 解析数据时出错 org.json.JSONException : Unterminated array at character

标签 android arrays json servlets arraylist

我是 JSON 的新手,不知道要将 Arraylist 成功转换为 JSONArray 需要遵循哪些约束。我正在尝试将多个字符串元素的数组列表从 servlet 发送到 JSON 解析器。当我有小元素时同样有效,例如[已解决,正在工作] 但如果数组元素包含空格或特殊字符,我的 JSON Parser 类会抛出错误。解析时出现以下错误:

08-20 16:46:13.480: E/JSON Parser(475): Error parsing data org.json.JSONException: Unterminated array at character 17 of [Closed, Emails not working in A-82 premises ; Requested Username: P Smith]

JSON 解析器代码:

public class JSONParser {

    static InputStream is = null;
    static JSONArray jArr = null;
    static String json = "";

 // constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            //HttpPost httpPost = new HttpPost(url);
            HttpGet httpPost = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jArr = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jArr;

    }
}

最佳答案

我有一个示例调用(只需要包含在 try-catch 中):

JSONArray array = new JSONArray(URLProcessing.GetUrl(path));

我将其保存在 Utils 类中:

public static String GetUrl(String url) throws Exception {
    URL serverAddress = null;
    HttpURLConnection connection = null;
    BufferedReader rd = null;
    StringBuilder sb = null;
    String line = null;

    try {
        serverAddress = new URL(url);
        // set up out communications stuff
        connection = null;
        // Set up the initial connection
        connection = (HttpURLConnection) serverAddress.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setReadTimeout(15000);
        connection.setConnectTimeout(15000);
        connection.connect();

        // read the result from the server
        rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        sb = new StringBuilder();

        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (Exception e) {
        throw e;
        // Swallow
    } finally {
        // close the connection, set all objects
        // to null
        connection.disconnect();
        rd = null;
        sb = null;
        connection = null;
    }
}

如果这对您来说有任何问题,那么我会说您有一些损坏的 JSON,您应该捕获它并将其扔到像 http://jsonlint.com/ 这样的网站上看看发生了什么……我已经使用这段代码一年多了,没有任何问题。

关于android - 解析数据时出错 org.json.JSONException : Unterminated array at character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18334300/

相关文章:

java - 如何从文件中读取用户定义的对象?

c - 访问函数内的二维数组

java - Jersey REST 字符编码

jquery - 从 MVC Controller 返回 JSON 字符串

Android:java.lang.SecurityException:没有 uri 0 @content://com.android.chrome.FileProvider/BlockedFile 的权限

android - 如何打开图库中的图片

python - 根据另一个 numpy 数组中的值查找 numpy 数组的索引

javascript - Jquery 复选框选择迭代

Android后台服务与任务队列

java - 如何从服务器端的客户端计算到给定点的最近坐标