Java 无法解析 jsonobject----“JSONObject 文本必须在“的字符 1 处以 '{' 开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24926739/
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
Cannot parse jsonobject----"A JSONObject text must begin with '{' at character 1 of "
提问by Ron
String json = "{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}";
try {
JSONObject jsonObject = JSONObject.fromObject(json);
String name = jsonObject.getString("name");
String address = jsonObject.getString("address");
System.out.println("name is:" + name);
System.out.println("address is:" + address);
JSONArray jsonArray = jsonObject.getJSONArray("array");
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println("item " + i + " :" + jsonArray.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Eveything is OK.
But when I put {'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}
into a file.
一切正常。但是当我放入{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}
一个文件时。
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null){
JSONObject jo = JSONObject.fromObject(tempString.trim());
String id = jo.getString("id");
String name = jo.getString("name");
log.info(id + ":" + name);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
It tells me Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of "{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}
. What's the problem in this code? Can any figure it out for me? Thanks.
它告诉我Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of "{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}
。这段代码有什么问题?有谁能帮我弄清楚吗?谢谢。
my file:
我的文件:
{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}
{'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}
采纳答案by McDowell
From the comment:
从评论:
I use
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8"); reader = new BufferedReader(isr); String tempString = null; while ((tempString = reader.readLine()) != null){
, and my file starts with
EE BB BF
, I've checked.
我用
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8"); reader = new BufferedReader(isr); String tempString = null; while ((tempString = reader.readLine()) != null){
,我的文件以 开头
EE BB BF
,我已经检查过了。
The issue is that the file starts with a BOM. The JSON decoder expects the file to begin with a character that can start a JSON type but it's getting U+FEFF.
问题是该文件以BOM开头。JSON 解码器期望文件以一个字符开头,该字符可以作为 JSON 类型的开头,但它会得到 U+FEFF。
It would be best if the JSON file did not start with a BOM.
最好 JSON 文件不以 BOM 开头。
If you must handle this case then you can do it with the buffer:
如果您必须处理这种情况,那么您可以使用缓冲区来完成:
BufferedReader buf = new BufferedReader(isr);
// remove BOM
buf.mark(1);
if(buf.read() != '\uFEFF') {
buf.reset();
}
// continue...
回答by Luke Grab-Bag English
If you look at the string returned in the error, " {'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}"
, you'll see that in character 1 there is actually a space. What happens if you try removing this space from your file?
如果您查看错误中返回的字符串, " {'name': 'Tom','array':[{'a':'111','b':'222','c':'333'},{},{'a':'999'}],'address':'York'}"
,您会看到在字符 1 中实际上有一个空格。如果您尝试从文件中删除此空间会发生什么?
回答by das Keks
You have to replace your single quotes '
with double quotes "
.
你必须'
用双引号替换你的单引号"
。
{
"name": "Tom",
"array": [
{
"a": "111",
"b": "222",
"c": "333"
},
{
},
{
"a": "999"
}
],
"address": "York"
}
As defined in the JSON specification http://www.ietf.org/rfc/rfc4627.txt
如 JSON 规范中所定义http://www.ietf.org/rfc/rfc4627.txt
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks.
字符串的表示类似于 C 系列编程语言中使用的约定。字符串以引号开始和结束。
And quotation mark
means "
并quotation mark
意味着"