Using Hasmap with collection of String
can work with data json who has the layout
of key and value, basic example:
Code:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
Gson gson = new Gson();
try (Reader reader = new FileReader("c:\\Temp\\arquivo.json"))
{
Type listType =
new TypeToken<HashMap<String, HashMap<String, List<String>>>>(){}.getType();
HashMap<String, HashMap<String, List<String>>> c = gson.fromJson(reader, listType);
HashMap<String, List<String>> get = c.get("profissao");
Set<String> items = get.keySet();
for(String item: items){
System.out.println(item);
System.out.println(get.get(item));
System.out.println("-----------------------");
}
}
catch (IOException e)
{
}
Exit:
run:
jornalista
[escritor, legal, fotografo]
-----------------------
maquinista
[senai, mecanico, articulado]
-----------------------
programador
[focado, exatas, articulado]
-----------------------
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)
References:
You wanted to read only or pass this content to a class?
– novic
move to a class would be better !
– Rodrigo Gabriel