我正在使用json-io将一些 JSON 数据转换为 Java 对象:
DataTransferContainer dataTransferContainer = (DataTransferContainer)JsonReader.jsonToJava(json);
DataTransferContainer 只是一个包含一些 GeoJson 元素的对象。这个问题没关系。
上面的代码在 Android 之外的 JUnit 测试中工作,但是当我在 Android 上运行它时,我得到了这个 RuntimeException:
12-07 20:12:51.260: E/AndroidRuntime(2002): FATAL EXCEPTION: AsyncTask #1
12-07 20:12:51.260: E/AndroidRuntime(2002): java.lang.RuntimeException: An error occured while executing doInBackground()
12-07 20:12:51.260: E/AndroidRuntime(2002): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.lang.Thread.run(Thread.java:1019)
12-07 20:12:51.260: E/AndroidRuntime(2002): Caused by: java.lang.ExceptionInInitializerError
12-07 20:12:51.260: E/AndroidRuntime(2002): at com.mydomain.myapp.dataprocessing.JsonConverter.jsonObjectToObject(JsonConverter.java:22)
12-07 20:12:51.260: E/AndroidRuntime(2002): at com.mydomain.myapp.dataprocessing.DataManagerTask.processDataFromRemote(DataManagerTask.java:104)
12-07 20:12:51.260: E/AndroidRuntime(2002): at com.mydomain.myapp.dataprocessing.DataManagerTask.doInBackground(DataManagerTask.java:56)
12-07 20:12:51.260: E/AndroidRuntime(2002): at com.mydomain.myapp.dataprocessing.DataManagerTask.doInBackground(DataManagerTask.java:18)
12-07 20:12:51.260: E/AndroidRuntime(2002): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-07 20:12:51.260: E/AndroidRuntime(2002): ... 4 more
12-07 20:12:51.260: E/AndroidRuntime(2002): Caused by: java.util.regex.PatternSyntaxException: Syntax error U_ILLEGAL_ARGUMENT_ERROR
12-07 20:12:51.260: E/AndroidRuntime(2002): (\d{2})[:.](\d{2})[:.](\d{2})[.](\d{1,10})([+-]\d{2}[:]?\d{2}|Z)?
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.regex.Pattern.compileImpl(Native Method)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.regex.Pattern.compile(Pattern.java:400)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.regex.Pattern.<init>(Pattern.java:383)
12-07 20:12:51.260: E/AndroidRuntime(2002): at java.util.regex.Pattern.compile(Pattern.java:374)
12-07 20:12:51.260: E/AndroidRuntime(2002): at com.cedarsoftware.util.io.JsonReader.<clinit>(JsonReader.java:114)
12-07 20:12:51.260: E/AndroidRuntime(2002): ... 10 more
在 JsonReader 中,第 114 行如下所示:
private static final Pattern timePattern1 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})[.](\\d{1,10})([+-]\\d{2}[:]?\\d{2}|Z)?");
我能够缩小范围。这是这部分:[:.]
它只是意味着“任何字符”或“冒号”。在 Android 上有什么问题?