user3326720 user3326720 - 4 months ago 14
JSON Question

How to get JsonArray from a Json File?

I have a JSon File in the structure:

[
{
"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
}
]


To preface, I'm using Gson to parse my Json file. What I want to be able to do is hold the data as a JsonArray

What I've written to do so is:

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


Which tells me
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $


My Land class:

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;

}

}


Why would I get this output from trying to get the array from a well-formatted Json file?

Answer

Problem is that

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

returns String representing location of resource, not content of resource. So you are trying to parse string like

file:/[your project location]/classes/res/LandInfo.json

which as you see is not valid JSON which is causing error.

To solve this problem you can use fromJson(Reader json, Class<T> classOfT). So create stream which will be responsible for reading data from your json file, wrap it with reader and pass it to fromJson method.

Your code can look like

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