Gson to Java - Map

Asked

Viewed 147 times

5

I need to pass these values from a json file to a java class file, 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 on android now and I must be forgetting or missing something very basic,anyone knows why inputStreamReader is not cool?

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

        Gson gson = new Gson(); 
        Items Items = gson.fromJson((BufferedReader) mapaNomes, Items.class);
  • 3

    This is Sopt, please translate your question into English.

  • 1

    I need that you perceive that this community speaks Portuguese.

  • 2

    I didn’t even know I was Portuguese,...

1 answer

3


First: your Reader is not reading from a file but from the standard input (System.in).

Second: you’re casting a Hashmap for a Bufferedreader, that’s the same thing as casting Puppy for Knob. It won’t work any more.

A way would be like this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.