Gson to Java - Map

5

I need to pass these values from a json file to a java class, the Json file is of this type:

{
        "id":1,
        "name":"Gold",
        "description":"Shiny!",
        "spriteId":1,
        "consumable":true,
        "effectsId":[1]
    },

I created a map this way:

Items i = new Items();


        Map<String, Items> mapaNomes = new HashMap<String, Items>();
        mapaNomes.put("Gold",i);
        mapaNomes.put("Apple",i );
        mapaNomes.put("Clain Mail",i );

The problem is when reading the json file, I started the program in android now and I must be forgetting or missing something very basic, does anyone know why the inputStreamReader is not legal?

BufferedReader in  = new BufferedReader(new InputStreamReader(System.in));

        Gson gson = new Gson(); 
        Items Items = gson.fromJson((BufferedReader) mapaNomes, Items.class);
    
asked by anonymous 23.10.2015 / 03:00

1 answer

3

First: your reader is not reading from a file, but from the default entry (System.in).

Second: you're casting from a HashMap to a BufferedReader, that's the same thing as casting Dog to Handle. It will not work either.

One way to do this would be:

BufferedReader in = new BufferedReader(new FileReader("arquivo.txt"));
Gson gson = new Gson(); 
Items items = gson.fromJson(in, Items.class);
mapaNomes.put(items.getNome(), items);
    
23.10.2015 / 13:22