沒關係,我可以寫一個例子,這個肖像,以您的問題..
我相信你的鑰匙將被改變,因爲你所問的問題是不一樣的,你真正想要的,所以我創建了Key
類,您可以將其更改爲您需要獲得的相應類Object
。
Key.java
import com.google.gson.annotations.SerializedName;
public class Key {
@SerializedName("key")
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
Data.java
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Data {
@SerializedName("someName")
private String someKey;
@SerializedName("Key")
private Key key;
@SerializedName("keys")
private ArrayList<Key> keys;
public String getSomeKey() {
return someKey;
}
public void setSomeKey(String someKey) {
this.someKey = someKey;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public ArrayList<Key> getKeys() {
return keys;
}
public void setKeys(ArrayList<Key> keys) {
this.keys = keys;
}
}
你可以用下面的代碼
public static void main(String[] args) {
// TODO Auto-generated method stub
Key key = new Key();
key.setKey("someNumber");
ArrayList<Key> keys = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Key key2 = new Key();
key2.setKey("1"+i);
keys.add(key2);
}
Data data = new Data();
data.setKey(key);
data.setKeys(keys);
String result =(new Gson()).toJson(data);
System.out.println(result);
System.out.println("\n#######\n");
Data data2 = (new Gson()).fromJson(result, Data.class);
System.out.println(data2.getKey().getKey());
}
測試這個3210
這只是一個例子,其中類Data
已被轉換爲JSON
而不是反序列化以獲得它的對象填充,這應該讓你瞭解如何讀取自己的數據。
輸出
{"Key":{"key":"someNumber"},"keys":[{"key":"10"},{"key":"11"},{"key":"12"},{"key":"13"},{"key":"14"}]}
#######
someNumber
10
11
12
13
14
檢查這個職位。我認爲它會回答你的問題;)。 http://stackoverflow.com/questions/26501968/deserializing-objects-automatically-to-a-bundle –