java “JSONArray 文本必须在 null 的字符 1 处以 '[' 开头”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3085830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:17:29  来源:igfitidea点击:

"A JSONArray text must start with '[' at character 1 of null"

javajson

提问by Skizit

Just wondering what that error could mean? I get it from the following code..

只是想知道这个错误可能意味着什么?我从以下代码中得到它..

 try{
    JSONArray jArray = new JSONArray(result);
    for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            Log.i("log_tag","id: "+json_data.getInt("id")+
                    ", name: "+json_data.getString("name")+
                    ", sex: "+json_data.getInt("sex")+
                    ", birthyear: "+json_data.getInt("birthyear")
            );
    }
}
}catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}

回答by Matt

Result is null, apparently. Or it simply does not start with [. Print it out before calling JSONArray to see.

结果显然是空的。或者它根本不以[. 在调用 JSONArray 之前打印出来看看。

Also take a look at the JSONArray source. It is expecting JavaScript array syntax, e.g.:

另请查看JSONArray 源。它期待 JavaScript 数组语法,例如:

['this', 'is', 'array', 'syntax']

['this', 'is', 'array', 'syntax']

回答by rahul

text.trim(); text.trim();

text.trim(); text.trim();

Text is the json file or string , do this at least two times one after another will resolve the problem 99.9%.

文本是json文件或字符串,至少一个接一个地做两次可以99.9%的解决问题。