`parseString(String)`方法在JsonParser类型中未定义。

huangapple go评论76阅读模式
英文:

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:

&lt;dependency&gt;
    &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
    &lt;artifactId&gt;gson&lt;/artifactId&gt;
    &lt;version&gt;2.8.6&lt;/version&gt;
&lt;/dependency&gt;

For Gradle, use:

compile group: &#39;com.google.code.gson&#39;, name: &#39;gson&#39;, version: &#39;2.8.6&#39;

For SBT, use:

libraryDependencies += &quot;com.google.code.gson&quot; % &quot;gson&quot; % &quot;2.8.6&quot;

For Ivy, use:

&lt;dependency org=&quot;com.google.code.gson&quot; name=&quot;gson&quot; rev=&quot;2.8.6&quot;/&gt;

For Grape, use:

@Grapes(
    @Grab(group=&#39;com.google.code.gson&#39;, module=&#39;gson&#39;, version=&#39;2.8.6&#39;)
)

For Leiningen, use:

[com.google.code.gson/gson &quot;2.8.6&quot;]

For Buildr, use:

&#39;com.google.code.gson:gson:jar:2.8.6&#39;

huangapple
  • 本文由 发表于 2020年8月27日 15:46:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63611414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定