【问题标题】:JSON array to ArrayList of specific class [duplicate]JSON数组到特定类的ArrayList [重复]
【发布时间】:2017-07-18 19:06:04
【问题描述】:

JSON

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

Java

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

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

【问题讨论】:

    标签: java gson


    【解决方案1】:

    试试这个:

    import com.google.gson.reflect.TypeToken;
    
    ArrayList<Product> products  = new Gson().fromJson(results, new TypeToken<ArrayList<Product>>(){}.getType());;
    

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2016-10-05
      • 1970-01-01
      相关资源
      最近更新 更多