如何从Json文件中获取JsonArray?

时间:2016-08-03 18:44:52

标签: java json

我在结构中有一个JSon文件:

[
    {
      "name": "north america",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "south america",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "north europe",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "west europe",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "east europe",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "south europe",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "north africa",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "south africa",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "north asia",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "west asia",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "east asia",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "southeast asia",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "south asia",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    },
    {
      "name": "oceania",
      "population": 10,
      "wealth": 0,
      "education": 0,
      "corruption": 0
    }   
]

前言,我正在使用Gson来解析我的Json文件。我想要做的是将数据保存为JsonArray

我写的是:

final Land[] landInfo = new Gson().fromJson(getClass().getResource("../res/LandInfo.json").toExternalForm(), Land[].class)

告诉我Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

我的土地类:

public class Land {

    private String name;

    private int population;

    private int wealth;

    private double education;

    private double corruption;



    public String getName() {

        return name;

    }



    public int getPopulation() {

        return population;

    }



    public void setPopulation(int population) {

        this.population = population;

    }



    public int getWealth() {

        return wealth;

    }



    public void setWealth(int wealth) {

        this.wealth = wealth;

    }



    public double getEducation() {

        return education;

    }



    public void setEducation(double education) {

        this.education = education;

    }



    public double getCorruption() {

        return corruption;

    }


    public void setCorruption(double corruption) {

        this.corruption = corruption;

    }

}

为什么我会尝试从格式良好的Json文件中获取数组?

1 个答案:

答案 0 :(得分:0)

问题在于

getClass().getResource("../res/LandInfo.json").toExternalForm()

返回表示资源的 location 的字符串,而不是资源的 content 。所以你试图解析像

这样的字符串
file:/[your project location]/classes/res/LandInfo.json

如您所见,它不是导致错误的有效JSON。

要解决此问题,您可以使用fromJson(Reader json, Class<T> classOfT)。因此创建流将负责从json文件中读取数据,用读取器包装并将其传递给fromJson方法。

您的代码可能看起来像

InputStream in = getClass().getResourceAsStream("../res/LandInfo.json");
Land[] landInfo = new Gson().fromJson(new InputStreamReader(in, "UTF-8"), Land[].class);