尝试解析 JSON 时变量名无效。

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

Trying to parse Json but invalid variable name

问题

![错误][1]][1]

我正在尝试解析Json数据,然而,即使在上一行中定义了jsonData变量,当我尝试将其插入到`JSONObject object = (JSONObject) new JSONParser().parse(jsonData);`这一行中时,它仍然显示为未声明。

我已经安装了这些依赖项:

```java
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

在打印后,我已经检查了Json数据的有效性。

请帮我解析这些数据。谢谢!


<details>
<summary>英文:</summary>

[![Error][1]][1]

I&#39;m trying to parse Json data however even after I define the jsonData variable in the line above, it shows up as undeclared when I try to insert it in the line `JSONObject object = (JSONObject) new JSONParser().parse(jsonData);`

I have these dependencies installed as well

    import org.json.simple.JSONObject;
    import org.json.simple.JSONArray;
    import org.json.simple.parser.ParseException;
    import org.json.simple.parser.JSONParser;

I have checked the Json data to be valid after printing it.

Please help me parse the data. Thanks

  [1]: https://i.stack.imgur.com/74wI2.jpg

</details>


# 答案1
**得分**: 0

你在循环块内部声明了`jsonData`变量。当运行时离开循环时,你的变量超出了范围。

你可以这样实现:

```java
var jsonData = "";
while((inputLine = in.readLine()) != null)
{
    jsonData += inputLine;
}

或者你可以使用一个StringBuilder

英文:

You declared your jsonData Variable inside a loop block. Your variable is out of scope when your runtime leaves the loop.

You would implement it like this:

var jsonData = &quot;&quot;;
while((inputLine = in.readLine()) != null)
{
    jsonData += inputLine;
}

Or you can use a StringBuilder

huangapple
  • 本文由 发表于 2020年9月30日 12:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131121.html
匿名

发表评论

匿名网友

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

确定