在Java中是否有一种方法可以从json中读取并直接将其值放入Collection中。在我的情况下,我有三种不同的类型。我可以做到:
Collection<Type> types;
JSON:
{
"type1":[
{
"version": "1.11",
"releaseDate": "2015-03-26",
"downloadUrl": "****"
},
{
"version": "1.12",
"releaseDate": "2015-04-08",
"downloadUrl": "****"
}],
"type2":[
{
"version": "2.20",
"releaseDate": "2015-03-26",
"downloadUrl": "****"
},
{
"version": "2.21",
"releaseDate": "2015-04-08",
"downloadUrl": "****"
}],
"type3":[
{
"version": "2.56",
"releaseDate": "2015-03-26",
"downloadUrl": "****"
},
{
"version": "2.57",
"releaseDate": "2015-04-08",
"downloadUrl": "****"
}]
}
答案 0 :(得分:0)
您可以使用Gson轻松完成此操作。我根据您的要求(例如数据类型,约定等)给您基本的想法改变...
public class YourType{
private String version;
private String releaseDate;
private String downloadUrl;
// getter-setter
}
和
/* Code to convert JSON to Collection */
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
Type objType = new TypeToken<List<YourType>>(){}.getType();
List<YourType> list = objGson.fromJson(yourJSONObject.getJSONArray("type1").toString(), objType);