Ormlite中使用Collection和Gson使用List

下面这段代码没有是问题的:

<span style="font-size:18px;">List<String> numbers = new ArrayList<String>();
		numbers.add("1");
		numbers.add("2");
		numbers.add("3");
		numbers.add("4");
		ArrayList<String> dss = (ArrayList<String>) numbers;
		for (String string : dss) {
			System.out.println(string);
		}</span>

实际需求如下,开始的时候使用gson解析一个

<span style="font-size:18px;">public class MyBean {
<span style="white-space:pre">	</span>public int id;
<span style="white-space:pre">	</span>public List<String> name;
}</span>
现在这个类需要做成表了。
<span style="font-size:18px;">@DatabaseTable(tableName = "mybean")
public class MyBean {
	@DatabaseField(generatedId = true)
	public int id;
	@ForeignCollectionField
	public Collection<String> name;
}</span>

(1)Ormlite使用不了List这样子的。http://stackoverflow.com/questions/17691507/storing-list-of-objects-using-datatype-serializable-in-ormlite-android

这个库作者的回答。

(2)那使用Collection的话,gson能正确解析吗?

http://stackoverflow.com/questions/15300179/how-to-convert-json-string-to-object-collection-using-gson

(3)发现其实并不需要使用下面这种方法。直接类型转化就可以实现了。

Type token = new TypeToken<Collection<UserObject>>() {}.getType(); Collection<UserObject> result = gson.fromJson(json, token);


你可能感兴趣的:(Ormlite中使用Collection和Gson使用List)