GSON反序列化捆绑 [英] GSON deserialize to bundle
问题描述
如何反序列化这个JSON:
data:{
key1:11,
key2:{
key3:22
key4:83
key5:99
}
}
到使用GSON库的Android Bundle?这是行不通的:
类模型实现Parcelable {
private int key1;
private Bundle key2;
[...]
保护模型(Parcel in){
key1 = in.readInt();
[...]
key2 = in.readBundle();
}
public void writeToParcel(Parcel dest,int flags){
dest.writeInt(key1);
[...]
dest.writeBundle(key2);
$ b我不想创建key2模型。 解决方案好的,我可以写一个例子来解释你的问题。
我相信你的密钥会改变,因为你在问题中提出的问题与你实际需要的不一样,所以我创建了一个 Key
类,并且您可以将其更改为您需要获取的适当类 Object
。
键.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;
$ block $ $ $ b $ p Data.java
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Data {
@SerializedName(someName)
private String someKey;
@SerializedName(Key)
私钥键;
@SerializedName(keys)
private ArrayList< Key>键;
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(){
返回键;
public void setKeys(ArrayList< Key> keys){
this.keys = keys;
你可以用下面的代码测试它
public static void main(String [] args){
// TODO自动生成的方法存根
Key key =新的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);
字符串结果=(new Gson())。toJson(data);
System.out.println(result);
System.out.println(\\\
####### \\\
);
Data data2 =(new Gson())。fromJson(result,Data.class);
System.out.println(data2.getKey()。getKey());
}
这只是一个例子,其中类 Data
已被转换为 JSON
,而不是反序列化以获取其对象,这应该让您了解如何读取自己的数据。
输出
{ 密钥:{ 键: someNumber}, 钥匙:[{ 键: 10},{ 键: 11},{ 键: 12 },{key:13},{key:14}]}
#######
$ b $ someNumber
10 b b 11 b b 12 b b 13 b b 14 b b b b c
$ bHow can i deserialize this JSON:
"data": {
"key1": 11,
"key2": {
"key3": 22
"key4": 83
"key5": 99
}
}
to an Android Bundle using GSON library? This is not working:
class Model implements Parcelable {
private int key1;
private Bundle key2;
[...]
protected Model(Parcel in) {
key1 = in.readInt();
[...]
key2 = in.readBundle();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(key1);
[...]
dest.writeBundle(key2);
}
}
I don't want to create a key2 model.
解决方案 ok, I can write an example to this likeness to your question..
I believe your keys would be changing as what you have asked in the question is not the same as what you actually want, so I created a Key
class and you can change it to appropriate class Object
which you need to get.
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;
}
}
you can test this with following code
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());
}
this is just an example where the class Data
has been converted in JSON
and than deserialized to get it's object populated, this should get you an idea about how to read your own data.
Output
{"Key":{"key":"someNumber"},"keys":[{"key":"10"},{"key":"11"},{"key":"12"},{"key":"13"},{"key":"14"}]}
#######
someNumber
10
11
12
13
14
这篇关于GSON反序列化捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!