解析数据时出错org.json.JSONException:字符处未终止的数组

时间:2013-08-20 11:51:57

标签: android arrays json servlets arraylist

我是JSON的新手,并不知道Arraylist成功转换为JSONArray需要遵循的约束。我试图将多个字符串元素的arraylist从servlet发送到JSON Parser。当我有小元素时,同样有效,例如[Resolved,Working]但是如果数组元素包含空格或特殊字符,则我的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;

    }
}

1 个答案:

答案 0 :(得分:2)

我有一个示例调用(只需要包含在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/这样的网站,看看发生了什么......我一直在使用这个代码超过一年,现在没有问题。