1 回答
TA贡献1775条经验 获得超11个赞
User你需要使用而不是类Map<String, Map<String, List<String>>>:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class GsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
final Gson gson = new Gson();
final Type map = new TypeToken<Map<String, Map<String, List<String>>>>(){}.getType();
final Map<String, Map<String, List<String>>> result = gson.fromJson(new FileReader(jsonFile), map);
System.out.println(result);
if (result != null) {
for (final Map.Entry<String, Map<String, List<String>>> entry : result.entrySet()) {
System.out.println("username: " + entry.getKey());
final Map<String, List<String>> user = entry.getValue();
System.out.println("transactions: " + user);
}
}
}
}
上面的代码打印:
{username1={456=[], 123=[], 789=[]}, username2={123=[], 456=[], 789=[]}, username3={789=[], 123=[], 456=[transaction10, transaction6, transaction9, transaction3]}}
username: username1
transactions: {456=[], 123=[], 789=[]}
username: username2
transactions: {123=[], 456=[], 789=[]}
username: username3
transactions: {789=[], 123=[], 456=[transaction10, transaction6, transaction9, transaction3]}
如果你真的需要,你可以User在解析后创建对象。
添加回答
举报