0

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?

Orange Receptacle
  • 1,173
  • 3
  • 19
  • 40
  • Maybe this will help you: http://stackoverflow.com/questions/3763937/gson-and-deserializing-an-array-of-objects-with-arrays-in-it – Klapsa2503 Aug 03 '16 at 18:52
  • I've tried changing my code by removing the "all" and having the whole Json file be encapsulated with [ ]. However, my error is similar, `Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $` – Orange Receptacle Aug 03 '16 at 19:03
  • I may be wrong but it seems that error states that parser found `"` where it expected `[` and it was at start of your JSON file. But based on your code and example it doesn't look right. Could you post proper JSON structure and code which will let us reproduce your problem? You don't need to post all objects from array, even one or two should be enough. – Pshemo Aug 03 '16 at 19:07
  • I've updated the Json text to mirror my original – Orange Receptacle Aug 03 '16 at 19:13
  • `Land[] landInfo = new Gson().fromJson(jsonStr, Land[].class);` works for me. (`jsonStr` is string holding JSON from your question). – Pshemo Aug 03 '16 at 19:16
  • I've added my Land class since there might be some confliction? As what you have written is near identical to mine and doesn't work – Orange Receptacle Aug 03 '16 at 20:30

1 Answers1

0

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);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Also `../res/LandInfo.json` doesn't look right. This path depends on location of class from which you are calling `getClass()` so if you will try to use this code in class like `com.your.package` you will need to add a lot of `../`. It is better to create separate package/folder, add it to your classpath (or depending on IDE buildpath) and place your `res` folder there. This way you should be able to access `res` folder via `/res` and all its contend via `/res/content`. – Pshemo Aug 03 '16 at 21:08