英文:
The method parseString(String) is undefined for the type JsonParser
问题
以下是您要的翻译内容:
我的代码之前运行得很好,但现在抛出了一个异常,
代码:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = JsonParser.parseString(resultJsonString);
而异常出现在这一行 JsonElement jsonElement = JsonParser.parseString(resultJsonString);
异常信息为:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parseString(String) is undefined for the type JsonParser
Any hints to why this is happening? It was working fine till yesterday!
英文:
My code was working perfectly before but now it is throwing out an exception,
The code :
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = JsonParser.parseString(resultJsonString);
and the exception in the line JsonElement jsonElement = JsonParser.parseString(resultJsonString);
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parseString(String) is undefined for the type JsonParser
Any hints to why this is happening? It was working fine till yesterday!
答案1
得分: 2
这更像是您将自己的类命名为与 Gson JsonParser
类相同的方式。因此,编译器会将 JsonParser
解析为您自己的类(your.package.JsonParser
),而不是 Gson JsonParser(com.google.gson.JsonParser)
。您可能希望重命名您的类,或者在每次引用该类时改用 com.google.gson.JsonParser
。
另一个可能性是您使用的 Gson
库可能早于 2.8.6
版本。如果是这种情况,您需要升级您的 Gson
库。静态方法 JsonParser.parseString
是在 2.8.6
版本中添加的。请查看变更日志。
对于 Maven,请使用:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
对于 Gradle,请使用:
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
对于 SBT,请使用:
libraryDependencies += "com.google.code.gson" % "gson" % "2.8.6"
对于 Ivy,请使用:
<dependency org="com.google.code.gson" name="gson" rev="2.8.6"/>
对于 Grape,请使用:
@Grapes(
@Grab(group='com.google.code.gson', module='gson', version='2.8.6')
)
对于 Leiningen,请使用:
[com.google.code.gson/gson "2.8.6"]
对于 Buildr,请使用:
'com.google.code.gson:gson:jar:2.8.6'
英文:
This looks more like you've named your own class the same way as the Gson JsonParser
class. So the compiler resolves JsonParser
to your own class (your.package.JsonParser
) and not to the Gson JsonParser (com.google.gson.JsonParser)
. You may want to rename your class or use com.google.gson.JsonParser
instead of JsonParser
each time you want to refer to this class.
Another possibility is that the Gson
library that you're using might be older than version 2.8.6
. If this is the case, you've to upgrade your Gson
lib. The static method JsonParser.parseString
is added in version 2.8.6
. Checkout the change log.
For Maven, use:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
For Gradle, use:
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
For SBT, use:
libraryDependencies += "com.google.code.gson" % "gson" % "2.8.6"
For Ivy, use:
<dependency org="com.google.code.gson" name="gson" rev="2.8.6"/>
For Grape, use:
@Grapes(
@Grab(group='com.google.code.gson', module='gson', version='2.8.6')
)
For Leiningen, use:
[com.google.code.gson/gson "2.8.6"]
For Buildr, use:
'com.google.code.gson:gson:jar:2.8.6'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论