gpt4 book ai didi

java - 如何使用带有@SerializedName 注释的gson 进行序列化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:00 25 4
gpt4 key购买 nike

这是我使用 Gson 进行序列化的第一种方法。我收到 facebook 对我的 android 应用程序的响应,如下所示:

     Result: {
Response: responseCode: 200,
graphObject: GraphObject{graphObjectClass=GraphObject,
state={
"data":[{"pic_square":"https:\/\/fbcdn-profile-a.akamaihd.net\/xxx.jpg",
"uid":"1020272xxxx852765","name":"Mister X"}
},
error: null, isFromCache:false
}]}

我创建了新类,但我不知道如何填写...

import java.io.Serializable;
import java.util.Arrays;
import com.google.gson.annotations.SerializedName;
public class FacebookResponse implements Serializable{

private static final long serialVersionUID = -104137709256566564L;

@SerializedName("data")
private FacebookResponse[] data;


@Override
public String toString() {
return "FacebookResponse [data=" + Arrays.toString(data) + "]";
}

在我的主要 fragment 中:

 Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
Log.i(TAG, "Result: " + response.toString());
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
FacebookResponse facebookResponse= gson.fromJson(response.toString(),FacebookResponse.class);
}
});
Request.executeBatchAsync(request);

非常感谢您的帮助

最佳答案

解决方案

String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN "
+ "(SELECT uid2 FROM friend WHERE uid1 = me() )";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session, "/fql", params, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.i(TAG, "Result: " + response.toString());

try {
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
**//here i get Data values**
JSONObject data = response.getGraphObject()
.getInnerJSONObject();
FacebookResponses facebookResponses = gson
.fromJson(data.toString(),
FacebookResponses.class);

Intent i = new Intent(getActivity()
.getApplicationContext(),
FacebookUsersImages.class);
i.putExtra("facebookResponses", facebookResponses);
startActivity(i);

// Log.i(TAG, "Result finale : " +
// facebookResponses.toString());
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Request.executeBatchAsync(request);

FacebookResponses 类

public class FacebookResponses implements Serializable {

private static final long serialVersionUID = 1L;

@SerializedName("data")
public FacebookRisp[] data;

public FacebookRisp[] getData() {
return data;
}

public void setData(FacebookRisp[] data) {
this.data = data;
}

@Override
public String toString() {
return "FacebookResponses [data=" + Arrays.toString(data) + "]";
}
}

FacebookRisp 类

public class FacebookRisp implements Serializable {

private static final long serialVersionUID = 1L;

@SerializedName("pic_square")
private String pic_square;

@SerializedName("pic")
private String pic;

@SerializedName("pic_big")
private String pic_big;

@SerializedName("pic_small")
private String pic_small;

@SerializedName("uid")
private String uid;

public String getPic() {
return pic;
}

public void setPic(String pic) {
this.pic = pic;
}

public String getPic_big() {
return pic_big;
}

public void setPic_big(String pic_big) {
this.pic_big = pic_big;
}

public String getPic_small() {
return pic_small;
}

public void setPic_small(String pic_small) {
this.pic_small = pic_small;
}

@SerializedName("name")
private String name;

public String getPic_square() {
return pic_square;
}

public void setPic_square(String pic_square) {
this.pic_square = pic_square;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "FacebookRisp [pic_square=" + pic_square + ", pic=" + pic + ", pic_big=" + pic_big + ", pic_small=" + pic_small + ", uid=" + uid
+ ", name=" + name + "]";
}
}

关于java - 如何使用带有@SerializedName 注释的gson 进行序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20799324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com