为了账号安全,请及时绑定邮箱和手机立即绑定

将 JSON 解析为对象

将 JSON 解析为对象

慕田峪9158850 2022-06-30 17:36:44
我有一个User类定义为:User.javapackage model;import java.util.List;import java.util.Map;public class User {    private final Map<String, List<String>> accountTransactionsMap;    public User(final Map<String, List<String>> accountTransactionsMap) {        this.accountTransactionsMap = accountTransactionsMap;    }    public Map<String, List<String>> getAccountTransactionsMap() {        return accountTransactionsMap;    }}我正在调用返回以下响应的 REST API:{     "username1":{        "456":[        ],      "123":[        ],      "789":[        ]   },   "username2":{        "123":[        ],      "456":[        ],      "789":[        ]   },   "username3":{        "789":[        ],      "123":[        ],      "456":[           "transaction10",         "transaction6",         "transaction9",         "transaction3"      ]   }}我希望能够解析响应并将其存储在User对象中。我尝试了以下方法:Test.javapublic class Test {    public static void main(final String[] args) {        final String response = "{\"username1\":{\"456\":[],\"123\":[],\"789\":[]},\"username2\":{\"123\":[],\"456\":[],\"789\":[]},\"username3\":{\"789\":[],\"123\":[],\"456\":[\"transaction10\",\"transaction6\",\"transaction9\",\"transaction3\"]}}";        final Gson gson = new Gson();        final Type map = new TypeToken<Map<String, User>>(){}.getType();        final Map<String, User> result = gson.fromJson(response, map);        System.out.println(result);        if (result != null) {            for (final Map.Entry<String, User> entry : result.entrySet()) {                System.out.println("username: " + entry.getKey());                final User user = entry.getValue();                System.out.println("transactions: " + user.getAccountTransactionsMap());            }        }    }}这会产生输出:{username1=model.User@80ec1f8, username2=model.User@1445d7f, username3=model.User@6a396c1e}username: username1transactions: nullusername: username2transactions: nullusername: username3transactions: null
查看完整描述

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在解析后创建对象。


查看完整回答
反对 回复 2022-06-30
  • 1 回答
  • 0 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信