JSON数组到特定类的ArrayList

时间:2017-02-28 00:11:20

标签: java gson

JSON

[
  {
    "id": 1,
    "name": "product new"
  },
  {
    "id": 2,
    "name": "test12345"
  },
  {
    "id": 3,
    "name": "product new"
  }
]

爪哇

 ArrayList<Product> products
 products = new Gson().fromJson(result.toString(), ArrayList<Product>.getClass());

如何将JSON数组转换为基于Product类的ArrayList?

1 个答案:

答案 0 :(得分:1)

试试这个:

import com.google.gson.reflect.TypeToken;

ArrayList<Product> products  = new Gson().fromJson(results, new TypeToken<ArrayList<Product>>(){}.getType());;