如何处理包含带有支架JSON字符串? [英] How to handle JSON that contains strings with bracket?

查看:152
本文介绍了如何处理包含带有支架JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建获得互联网(通过AsyncTask的)一个JSON文件的活动。
我遇到这个错误: org.json.JSONException:在字符未终止的字符串...

我检查,我在应用内下载的JSON文件,我发现有一个包含括号的对象。这里是涉嫌JSON对象:

[{...} {ID:5674563646,猫:未分类,SUBCAT:未分类,名:天工实验室,说明:我们运行8-10pm和[最]周六12-6pm,地址开放时间:主街,城市:纽约,国家:美国 }
{...}]

那么,如何从我的活动逃脱括号?

下面是我下载JSON文件的AsyncTask的一部分:

 保护字符串doInBackground(字符串的URL ...){
    INT超时= 10;
    INT I,计数= 0;    BasicHttpParams basicParams =新BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(basicParams,超时* 1000);
    HttpConnectionParams.setSoTimeout(basicParams,超时* 1000);
    DefaultHttpClient客户端=新DefaultHttpClient(basicParams);    StringBuilder的StringBuilder的=新的StringBuilder();
    对于(i = 0; I< urls.length;我++){
        HTTPGET请求=新HTTPGET(网址[I]);
        request.addHeader(缓存控制,无缓存);        尝试{
            HTT presponse响应= client.execute(请求);
            HttpEntity实体= response.getEntity();
            InputStreamReader的在=新的InputStreamReader(entity.getContent());
            读者的BufferedReader =新的BufferedReader(中);
            串线=;            而((行= reader.readLine())!= NULL){
                stringBuilder.append(线);                算上++;
            }
        }赶上(例外五){
            e.printStackTrace();
            返回null;
        }
        publishProgress(计数* 100 / urls.length);
    }
    返回stringBuilder.toString();
}保护无效onPostExecute(字符串结果){
    super.onPostExecute(结果);...
    JSONArray JA =新JSONArray(结果);
...


解决方案

JSON响应,通常含有两种括号 {} [] 但无论是支架具有完全不同的意义和用法。同时,第一个重presents JSON对象,第二个再presents JSON阵列。

在您下载的JSON文件,该问题无关的 [大多数] 词,因为它是在双引号。在你的JSON输入的问题是地址:主街。开场双引号丢失在那里。

修正JSON输入:

  {ID:5674563646,猫:未分类,SUBCAT:未分类,名:天工实验室,说明: 我们从8-10pm和12-6pm [大多数]星期六运行开放时间,地址:主街,城市:纽约,国家:美国}

I'm creating an activity that get a JSON file from internet (via AsyncTask). I'm experiencing this error: org.json.JSONException: Unterminated string at character...

I checked the JSON file that I downloaded inside the app and I discovered that there is an object that contains brackets. Here is the suspected JSON object:

[{ ... }{"id":"5674563646","cat":"Uncategorized","subcat":"Uncategorized","name":"Tecno Lab","desc":"We run open hours from 8-10pm and [most] Saturdays from 12-6pm.","addr":"Main Street","city":"New York","country":"United States"} { ... }]

So, how to escape the brackets from my activity?

Here is the part of AsyncTask in which I download the JSON file:

protected String doInBackground(String... urls) {
    int timeout = 10;
    int i, count = 0;

    BasicHttpParams basicParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(basicParams, timeout * 1000);
    HttpConnectionParams.setSoTimeout(basicParams, timeout * 1000 );
    DefaultHttpClient client = new DefaultHttpClient(basicParams);

    StringBuilder stringBuilder = new StringBuilder();
    for (i = 0; i < urls.length; i++) {
        HttpGet request = new HttpGet(urls[i]);
        request.addHeader("Cache-Control", "no-cache");

        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            InputStreamReader in = new InputStreamReader(entity.getContent());
            BufferedReader reader = new BufferedReader(in);
            String line = "";

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);

                count++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        publishProgress(count * 100 / urls.length);
    }
    return stringBuilder.toString();
}

protected void onPostExecute(String result) {
    super.onPostExecute(result);

...
    JSONArray ja = new JSONArray(result);
...

解决方案

JSON response normally contains two kinds of brackets {} and [] but both the brackets have quite different meaning and use. While, the first one represents JSON Object, the second one represents JSON Array.

In the JSON file you downloaded, the problem has nothing to do with the [most] word as it is inside double quotes. The problem in your JSON input is "addr": "Main Street". The opening double quote is missing there.

Corrected JSON input:

{"id":"5674563646","cat":"Uncategorized","subcat":"Uncategorized","name":"Tecno Lab","desc":"We run open hours from 8-10pm and [most] Saturdays from 12-6pm.","addr":"Main Street","city":"New York","country":"United States"}

这篇关于如何处理包含带有支架JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆