如何在Java中将JSON数组转换为ArrayList <classtype>?

时间:2019-02-25 13:17:57

标签: java arrays json arraylist gson

这是我的输入JSON,我必须从中输入interLockingGrids并将其存储到ArrayList<ClassType>中。我已经进行了足够的搜索,但没有找到我想要的东西。有没有最简单的方法?

{"entity":"MasterGrid",

"data" :"{\"name\":\"MA_Pune\",
\"coordinates\" : [
{\"lat\":18.553449789265677,\"lng\":73.77419574114515},{\"lat\":18.554232972087224,\"lng\":73.77367002817823},{\"lat\":18.556135000463573,\"lng\":73.77144913093571},{\"lat\":18.555148376025496,\"lng\":73.7700973257704},{\"lat\":18.55309341985624,\"lng\":73.77075145899971},{\"lat\":18.5530421437421,\"lng\":73.77312207276918} ] ,
\"interLockingGrids\": [
{\"name\":\"Test1\",\"type\":\"CLUSTER\",\"coordinates\":[{\"lat\":18.555151666563823,\"lng\":73.7700902617189},{\"lat\":18.556135000463573,\"lng\":73.77144913093571},{\"lat\":18.556135000463573,\"lng\":73.77144913093571},{\"lat\":18.556135000463573,\"lng\":73.77144913093571},{\"lat\":18.556135000463573,\"lng\":73.77144913093571},{\"lat\":18.556135000463573,\"lng\":73.77144913093571}],\"isActive\":true,\"colorCode\":\"#D0983C\"} ,
{\"name\":\"Test2\",\"type\":\"CLUSTER\",\"coordinates\":[{\"lat\":18.5530421437421,\"lng\":73.77312207276918},{\"lat\":18.553449789265677,\"lng\":73.77419574114515},{\"lat\":18.55238201763598,\"lng\":73.7743998478852}],\"isActive\":true,\"colorCode\":\"#8E75B7\"}  ] ,

"operation":"GRID_OPERATION",
"description":"Baner_Test",
"submittedBy":"m@abc.com",
"submittedById":4,
"submittedByType":"OPS" }

1 个答案:

答案 0 :(得分:0)

在最近的项目中,我做了类似的事情:

public List<MyType> parseJsonToListOfMyType(String json) {
        Gson gson = new Gson();
        JsonArray job = gson.fromJson(json, JsonArray.class);
        Type listType = new TypeToken<List<MyType>>(){}.getType();
        return gson.fromJson(job, listType);
    }