gpt4 book ai didi

java - 如何修复 java.lang.IllegalStateException : Expected BEGIN_ARRAY but was STRING in cmd windows Gson, Apache Spark

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:49 24 4
gpt4 key购买 nike

我有这段代码:

File jsonFile = Paths.get(jsonPath).toFile();
BufferedReader bufferedReader = new BufferedReader(new FileReader(jsonFile));

Type collectionType = new TypeToken<List<Pattern>>(){}.getType();
List<Pattern> listPatterns = new Gson().fromJson(bufferedReader, collectionType);

当我在 IntelliJ 上运行它时,它成功了,但在构建为 jar 文件并在 Windows CMD 中通过 Spark-submit 运行后,出现错误:

spark-submit --class com.bigdata.main.Application --master local[1] target\extraction.jar

错误:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1
at com.google.gson.Gson.fromJson(Gson.java:815)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.bigdata.main.Application.main(Application.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.spark.deploy.JavaMainApplication.start(SparkApplication.scala:52)
at org.apache.spark.deploy.SparkSubmit.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:849)
at org.apache.spark.deploy.SparkSubmit.doRunMain$1(SparkSubmit.scala:167)
at org.apache.spark.deploy.SparkSubmit.submit(SparkSubmit.scala:195)
at org.apache.spark.deploy.SparkSubmit.doSubmit(SparkSubmit.scala:86)
at org.apache.spark.deploy.SparkSubmit$$anon$2.doSubmit(SparkSubmit.scala:924)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:933)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:338)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:803)
... 14 more

我的json文件在IntelliJ和Centos linux命令中运行正常,但在Windows上运行时出现错误。

Json 内容:

[
{
"bankName": "Bank1",
"Patterns": [
{
"category": "daily",
"type": "",
"pattern": "pattern_regex_1"
}
]
},
{
"bankName": "Bank2",
"Patterns": [
{
"category": "daily",
"type": "",
"pattern": "pattern_regex_2"
},
{
"category": "daily",
"type": "",
"pattern": "pattern_regex_3"
},
{
"category": "visa",
"type": "",
"pattern": "pattern_regex_4"
},
{
"category": "visa",
"type": "",
"pattern": "pattern_regex_5"
}
]
}
]

感谢您的支持!

最佳答案

谢谢@vatsalmevada 先生,因为他给了我正确的答案!

用途:

String text = new String(Files.readAllBytes(Paths.get("file")), StandardCharsets.UTF_8);

转换

关于java - 如何修复 java.lang.IllegalStateException : Expected BEGIN_ARRAY but was STRING in cmd windows Gson, Apache Spark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com