1

im learning parcing and got a problem trying to make it happen. found a lot of similar questions and more solutions but nothing helped me

thats my json

{
  "firstName": "name",
  "lastName": "last",
  "resistances": {
    "a1": 1,
    "a2": 2,
    "a3": 3,
    "a4": 4
  }
}

Player.class

class Player implements Serializable{

    String firstName;
    String lastName;
    int[] resistances;

    public Player(String firstName, String lastName, int[] resistances) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.resistances = resistances;
    }
}

and how i try to parce

Gson gson = new Gson();
Player player = gson.fromJson("test.json", Player.class);

SOS

3
  • 1
    the third variable will be an object.. resistances.. Commented Feb 17, 2018 at 20:01
  • and the outermost json pbj must have a name.. Commented Feb 17, 2018 at 20:03
  • For "resistances" to be an array it should be: "resistances": [ 1, 2, 3, 4 ] so the error message is because it is finding the string "a1" Commented Feb 17, 2018 at 20:33

2 Answers 2

1

fromJson(String, Class) parses the String itself, in your case test.json, not the file it represents. You need to create a Reader from the file you are trying to parse and pass it as first argument.

If test.json is in the external storage:

String fpath = Environment.getExternalStorageDirectory() + "/path/to/test.json";
 try {
        FileReader fr = new FileReader(fpath));
        gson.fromJson(fr, Player.class);
} catch (Exception e1) {
        e1.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I would have done this from the beginning, but it depends on the location of your file. If test.json is in the raw resource folder you can use new InputStreamReader(getResources().openRawResource(R.raw.test)) (the extension is ignored). If it's on the sd card, the answer is a bit longer.
thank you very much! its working! one more question - what is the difference between putting .json file is assets folder or raw ?
tyvm again! can i give you +karma ? :D
1

Your JSON "resistances" object should look like this for your parsing code to work:

{
  "firstName": "name",
  "lastName": "last",
  "resistances": [
    1, 2, 3, 4
  ]
}

Or, change the resistances variable in Java to a Map. Look here for a previous answer - GSON can parse a dictionary JSON object to a Map.

class Player implements Serializable {

    String firstName;
    String lastName;
    Map<String, Integer> resistances;

    public Player(String firstName, String lastName, Map<String, Integer> resistances) {
        this.firstName   = firstName;
        this.lastName    = lastName;
        this.resistances = resistances;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.