我一直在尝试解密json数据,然后使用GSON将其转换为Scala案例类。
代码:
import com.google.gson.Gson
object DecryptQRData extends App {
val key = "klix8TW3SMGtHLVO0ZbhwO8ggW0p+npHfB71epkvmE0="
val data = "c3NbCs3KQ6uC9d1kplSOg78opMs5+jKB/n5O3D4ttLQaNfWeU/s3RxYqDMazA09gOhhUWkRHZo3xrsgv/x2zoQ=="
val bytesArray: Array[Byte] = AES.decodeBase64(data)
val finalData: Array[Byte] = AES.decrypt(bytesArray, key)
val jsonString: String = new String(finalData)
println(s"result: $jsonString")
//result: "{\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c5c6c7e4c3c9c5cdc88ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}"
val gson: Gson = new Gson()
val extractedJson: User = gson.fromJson(jsonString, classOf[User])
println(s"user is: ${extractedJson}")
}
case class User(username: String, address: String)
上面的代码失败并出现以下异常:
result: "{\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d0d3d2f1d6dcd0d8dd9fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}"
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at DecryptQRData$.delayedEndpoint$DecryptQRData$1(DecryptQRData.scala:15)
at DecryptQRData$delayedInit$body.apply(DecryptQRData.scala:3)
at scala.Function0.apply$mcV$sp(Function0.scala:39)
at scala.Function0.apply$mcV$sp$(Function0.scala:39)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
at scala.App.$anonfun$main$1$adapted(App.scala:80)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.App.main(App.scala:80)
at scala.App.main$(App.scala:78)
at DecryptQRData$.main(DecryptQRData.scala:3)
at DecryptQRData.main(DecryptQRData.scala)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
... 15 more
Process finished with exit code 1
但是当我更换时
val jsonString: String = new String(finalData)
//jsonString contains "{\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3657545576515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}"
与
val jsonString: String = "{\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64050607240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}"
,我的程序运行并打印 User
.
问题:为什么要替换 jsonString
当两者包含相同数据时,实际数据有效。
编辑1:我尝试了下面的代码(删除了开头和结尾的引号)
val jsonString: String = new String(finalData)
println(s"jsonString: $jsonString")
val removedQuotes = jsonString.replaceAll("^\"|\"$", "")
println(s"removedQuotes: ${removedQuotes}")
val gson: Gson = new Gson()
val extractedJson: User = gson.fromJson(removedQuotes, classOf[User])
println(s"user is: ${extractedJson}")
但是,它也失败并出现以下错误:
jsonString: "{\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0b08092a0d070b030644090507" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}"
removedQuotes: {\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a2b28290a2d272b232664292527" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $.
最佳答案
这是因为您的 JSON
有效负载如下所示:
"{"username": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91f0f3f2d1f6fcf0f8fdbff2fefc" rel="noreferrer noopener nofollow">[email protected]</a>","address": "!Earth"}
注意开头的 "
。当您删除第一个 "
时,它将开始工作。注意下面几行:
System.out.println("\"{}\"");
System.out.println("{}");
打印:
"{}"
{}
编辑
System.out.println("Start point: " + json);
json = json.substring(1, json.length() - 1);
System.out.println("Get without \": " + json);
json = json.replaceAll("\\\\\"", "\"");
System.out.println("Valid: " + json);
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user);
上面的代码打印:
Start point: "{\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65040706250208040c094b060a08" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}"
Get without ": {\"username\": \"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e7e4e5c6e1ebe7efeaa8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>\",\"address\": \"!Earth\"}
Valid: {"username": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe9f9c9dbe99939f9792d09d9193" rel="noreferrer noopener nofollow">[email protected]</a>","address": "!Earth"}
User{username='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e8ebeac9eee4e8e0e5a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>', address='!Earth'}
另请参阅:
关于JsonSyntaxException Gson : Expected BEGIN_OBJECT but was STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007127/