如何在JSON字符串的日期时间值内转义冒号 [英] How to escape a colon inside a datetime value in a JSON string
本文介绍了如何在JSON字符串的日期时间值内转义冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用从外部源收到的字符串实例化JSONObject。该字符串包含日期时间值,而日期时间值又包含冒号。
当我尝试创建JSONObject的实例时,出现错误,它看起来像JSON不喜欢日期时间值中间的冒号。
I need to instantiate a JSONObject with a string that I receive from an external source. The string contains a datetime value, which in turn contains a colon. When I try to create an instance of the JSONObject, I get an error, it looks like JSON does not like the colon in the middle of the date time value.
这是一个代码段:
@Test
public void testGetDate()
{
String jsonStr = "{\"sDate\":2013-06-15T09:30:09+0000}";
try
{
JSONObject jsonObject = new JSONObject(jsonStr);
System.out.println(jsonObject.get("sDate"));
} catch (JSONException e)
{
e.printStackTrace();
}
}
我得到的错误是:
org.json.JSONException: Expected a ',' or '}' at 23 [character 24 line 1]
有人遇到过吗?有什么办法可以逃脱冒号?
Has anyone encountered this ? Is there some way to escape the colon?
推荐答案
如果将日期/时间对象括在双引号中,则应该接受它。
If you surround your date/time object in double quotes, it should accept it.
这应该可行:
String jsonStr = "{\"sDate\":\"2013-06-15T09:30:09+0000\"}";
这篇关于如何在JSON字符串的日期时间值内转义冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文