【问题标题】:Error parsing data org.json.JSONException: Unterminated array at character解析数据时出错 org.json.JSONException:字符处未终止的数组
【发布时间】:2013-08-22 11:02:18
【问题描述】:

我是 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;

    }
}

【问题讨论】:

  • 使用在线 JSON 验证器来验证您的 JSON 并在一切正常时做出响应,之后我们将检查更多错误
  • 如何生成数组列表的 JSON 以便我可以通过验证器运行它?
  • 在上面的代码中,您正在从 URL 中检索 JSON。这个 JSON 你必须验证
  • 使用上面的例子 if array = [Resolved,Working] 然后 JSONArray = ["Resolved","Working"] 这很好。但是如果 array = [Closed, Emails not working] 那么 JSONArray 返回为 null。我没有得到合适的 JSONArray 来通过验证器。
  • 在您接触之前验证您直接从您的服务获得的 JSON,以查看 JSON 是否正确。不是 JSON > Android > Array > Json > 验证器。而是执行服务 > JSON > JSON Validator。

标签: android arrays json servlets arraylist


【解决方案1】:

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

【讨论】:

  • 使用您上面提供的代码后,我仍然收到上述异常。它还给了我一大堆“com.facebook.katana”例外。是否有一些您使用的库我没有添加?由于某种原因,没有空格的元素成功地用“”包围,但任何空格和 JSONArray 都无法用“”包围元素。
  • 我在某处读到 JSON 需要正确转义新行。我们该怎么做?
  • 我的问题出现在两个方面:新行和空格
  • 您能否发布一个返回您尝试解析的 JSON 的 url 示例?这是您在服务器端生成的东西吗?听起来你有一些糟糕的json。我认为那些 facebook 异常与此无关。
  • URL 不返回 JSON,它返回一个数组。该数组正在使用 JSON Parser 类在我的 android 应用程序中转换为 JSON。我有一个服务器端 Web 服务 (servlet),它以以下格式返回一个数组:[已解决,电子邮件不起作用]。当我使用 JSON Parser 类转换它时,我收到错误:org.json.JSONException: Unterminated array at character 17 --> 这会根据空格或新行的出现而变化。
猜你喜欢
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 2014-06-03
  • 2013-08-20
相关资源
最近更新 更多