猿问

将 JSON 解析为对象

我有一个User类定义为:


User.java


package 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.java


public 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: username1

transactions: null

username: username2

transactions: null

username: username3

transactions: null



慕田峪9158850
浏览 142回答 1
1回答

繁星淼淼

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 {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; File jsonFile = new File("./resource/test.json").getAbsoluteFile();&nbsp; &nbsp; &nbsp; &nbsp; final Gson gson = new Gson();&nbsp; &nbsp; &nbsp; &nbsp; final Type map = new TypeToken<Map<String, Map<String, List<String>>>>(){}.getType();&nbsp; &nbsp; &nbsp; &nbsp; final Map<String, Map<String, List<String>>> result = gson.fromJson(new FileReader(jsonFile), map);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(result);&nbsp; &nbsp; &nbsp; &nbsp; if (result != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (final Map.Entry<String, Map<String, List<String>>> entry : result.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("username: " + entry.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Map<String, List<String>> user = entry.getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("transactions: " + user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}上面的代码打印:{username1={456=[], 123=[], 789=[]}, username2={123=[], 456=[], 789=[]}, username3={789=[], 123=[], 456=[transaction10, transaction6, transaction9, transaction3]}}username: username1transactions: {456=[], 123=[], 789=[]}username: username2transactions: {123=[], 456=[], 789=[]}username: username3transactions: {789=[], 123=[], 456=[transaction10, transaction6, transaction9, transaction3]}如果你真的需要,你可以User在解析后创建对象。
随时随地看视频慕课网APP

相关分类

Java
我要回答