首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >没有正确创建JSONObject (可能是JSON格式错误?)

没有正确创建JSONObject (可能是JSON格式错误?)
EN

Stack Overflow用户
提问于 2013-06-28 22:00:37
回答 3查看 727关注 0票数 0

我得到了这个JSON字符串:

代码语言:javascript
复制
[
{
    "id": 135,
    "date": "2013-08-30 19:00:29",
    "timestamp": "2013-08-30 19:00:29",
    "lat": "54.328274",
    "long": "-2.747215",
    "strap": "annual International Festival of Street Arts",
    "link": "http://dev.website.co.uk//?p=135",
    "title": "Title"
}
]

我确信这是正确的JSON语法(在iOS应用程序中工作得很好),但是当解析到JSONObject时,它捕获了一个错误。Java:

代码语言:javascript
复制
public static JSONObject getJSONfromURL(String url){

    //initialize
    InputStream is = null;   
    String result = "";   
    JSONObject jArray = null;

    //http post
    try {

        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(url);    
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        is = entity.getContent();    

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());   
    }
    //convert response to string

    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();
        result=sb.toString();

    } catch (Exception e) {    
        Log.e("log_tag", "Error converting result "+e.toString());
    }
    //try parse the string to a JSON object

    try {
        Log.d("log_tag", "jresult: " + result + "finish");
        jArray = new JSONObject(result);

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return jArray;
}

JSON中的某个地方是否存在错误?

EN

Stack Overflow用户

发布于 2013-06-28 22:04:03

当json字符串以[开头时,它将被视为需要执行new JSONArray()JSONArray

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17366459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档