由于字符串中的斜杠,JSONArray 的字符串给出异常
String to JSONArray giving exception because of slash in string
我正在尝试将 json 字符串转换为 JSONArray
org.json.JSONArray class,字符串包含带正斜杠的日期格式,但由于字符串中包含斜杠,我收到以下异常。
public static void main(String args[]) throws JSONException{
String jsonString = "[{ID:1, Name:Ann, DOB:14/08/1991}, {ID:2, Name:Vann, DOB:14/08/1992}]";
JSONArray jsonArray = new JSONArray(jsonString);
System.out.println(jsonArray.toString());
}
Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at 25 [character 26 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONObject.<init>(JSONObject.java:230)
at org.json.JSONTokener.nextValue(JSONTokener.java:380)
at org.json.JSONArray.<init>(JSONArray.java:118)
at org.json.JSONArray.<init>(JSONArray.java:147)
at com.s4m.sftp.service.impl.SFTPServiceImpl.main(SFTPServiceImpl.java:1150)
JSON
中的字符串值
字符串应该被引用...包括属性名称。见 the JSON specification:
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string.
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks.
JSON
中的日期值
JSON 没有 date
类型的表示。它必须表示为字符串。有关详细信息,请参阅 this answer。
在线JSON验证者
您可以使用online JSON validators来检查有效性。
有效JSON
[
{
"ID":1,
"Name":"Ann",
"DOB":"14/08/1991"
},
{
"ID":2,
"Name":"Vann",
"DOB":"14/08/1992"
}
]
但是,我会使用格式 yyyy-mm-dd
作为日期值而不是 dd/mm/yyyy
。
有效 JSON 作为 Java 字符串
String jsonString =
"[{\"ID\":1,\"Name\":\"Ann\",\"DOB\":\"14/08/1991\"},{\"ID\":2,\"Name\":\"Vann\",\"DOB\":\"14/08/1992\"}]";
字符串应该用双引号引起来。
尝试使用下面的 JSON 字符串
[{"ID":1, "Name":"Ann", "DOB":"14/08/1991"}, {"ID":2 , "Name":"Vann", "DOB":"14/08/1992"}]
将所有字段的类型保持为字符串。使用 toString 转换或只发送一个 RFC 1123 日期字符串 ToString("r") 来解析
我正在尝试将 json 字符串转换为 JSONArray org.json.JSONArray class,字符串包含带正斜杠的日期格式,但由于字符串中包含斜杠,我收到以下异常。
public static void main(String args[]) throws JSONException{
String jsonString = "[{ID:1, Name:Ann, DOB:14/08/1991}, {ID:2, Name:Vann, DOB:14/08/1992}]";
JSONArray jsonArray = new JSONArray(jsonString);
System.out.println(jsonArray.toString());
}
Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at 25 [character 26 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONObject.<init>(JSONObject.java:230)
at org.json.JSONTokener.nextValue(JSONTokener.java:380)
at org.json.JSONArray.<init>(JSONArray.java:118)
at org.json.JSONArray.<init>(JSONArray.java:147)
at com.s4m.sftp.service.impl.SFTPServiceImpl.main(SFTPServiceImpl.java:1150)
JSON
中的字符串值字符串应该被引用...包括属性名称。见 the JSON specification:
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string.
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks.
JSON
中的日期值JSON 没有 date
类型的表示。它必须表示为字符串。有关详细信息,请参阅 this answer。
在线JSON验证者
您可以使用online JSON validators来检查有效性。
有效JSON
[
{
"ID":1,
"Name":"Ann",
"DOB":"14/08/1991"
},
{
"ID":2,
"Name":"Vann",
"DOB":"14/08/1992"
}
]
但是,我会使用格式 yyyy-mm-dd
作为日期值而不是 dd/mm/yyyy
。
有效 JSON 作为 Java 字符串
String jsonString =
"[{\"ID\":1,\"Name\":\"Ann\",\"DOB\":\"14/08/1991\"},{\"ID\":2,\"Name\":\"Vann\",\"DOB\":\"14/08/1992\"}]";
字符串应该用双引号引起来。
尝试使用下面的 JSON 字符串
[{"ID":1, "Name":"Ann", "DOB":"14/08/1991"}, {"ID":2 , "Name":"Vann", "DOB":"14/08/1992"}]
将所有字段的类型保持为字符串。使用 toString 转换或只发送一个 RFC 1123 日期字符串 ToString("r") 来解析