问题描述
这是我的模型类
public class TwitterUser {
@Expose
public String gid;
public String icon_url;
public Boolean is_app_user;
@Expose
public String displayName;
public TwitterUser(String l,String i,String url,布尔app_user){
gid = i;
displayName = l;
icon_url = url;
is_app_user = app_user;
}
public TwitterUser(String l,String i){
gid = i;
displayName = l;
}
public String getGid(){
return gid;
}
public void setGid(String gid){
this.gid = gid;
}
public String getIcon_url(){
return icon_url;
}
public void setIcon_url(String icon_url){
this.icon_url = icon_url;
}
public Boolean getIs_app_user(){
return is_app_user;
}
public void setIs_app_user(Boolean is_app_user){
this.is_app_user = is_app_user;
}
public String getDisplayName(){
return displayName;
}
public void setDisplayName(String displayName){
this.displayName = displayName;
}
以下是我如何创建json请求
Gson gson = new GsonBuilder()。excludeFieldsWithoutExposeAnnotation()。create();
gson.toJson(twitterUser));
但是,当我将请求发送到服务器时,订单将被拒绝。我必须改变请求的字段顺序以保持:
-
gid
-
displayName
Gith 不支持支持定义属性,有没有什么方法可以实现这一点。
订购开箱即用 ,但还有其他库可以提供。 杰克逊允许使用,例如。当然,Gson有它的方式所以你可以通过创建你自己的Json序列化程序来实现它:
public class TwitterUserSerializer实现了JsonSerializer< TwitterUser> {
@Override
public JsonElement serialize(TwitterUser twitterUser,Type type,JsonSerializationContext context){
JsonObject object = new JsonObject();
object.add(gid,context.serialize(twitterUser.getGid());
object.add(displayName,context.serialize(twitterUser.getDisplayName());
// ...
返回对象;
}
}
然后当然你需要在安装期间将这个序列化程序传递给Gson,如下所示:
Gson gson = new GsonBuilder()。registerTypeAdapter (TwitterUser.class,新的TwitterUserSerializer())。excludeFieldsWithoutExposeAnnotation()。create();
String json = gson.toJson(twitterUser);
另请参阅:
ul>
I have integrated Gson to create the json used in a request for an android application.
Here is my model class
public class TwitterUser {
@Expose
public String gid;
public String icon_url;
public Boolean is_app_user;
@Expose
public String displayName;
public TwitterUser(String l, String i, String url, Boolean app_user) {
gid = i;
displayName = l;
icon_url = url;
is_app_user = app_user;
}
public TwitterUser(String l, String i) {
gid = i;
displayName = l;
}
public String getGid() {
return gid;
}
public void setGid(String gid) {
this.gid = gid;
}
public String getIcon_url() {
return icon_url;
}
public void setIcon_url(String icon_url) {
this.icon_url = icon_url;
}
public Boolean getIs_app_user() {
return is_app_user;
}
public void setIs_app_user(Boolean is_app_user) {
this.is_app_user = is_app_user;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
Here is how i create the json request
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
gson.toJson(twitterUser));
But when I send the request to the server - the order will be rejected. I have to change the request's field order to stay:
gid
displayName
but gson creates other way around, is there any way to achieve this.
Gson doesn't support definition of property order out of the box, but there are other libraries that do. Jackson allows defining this with @JsonPropertyOrder
, for example.
But of course Gson has it's way so you can do it by creating your very own Json serializer:
public class TwitterUserSerializer implements JsonSerializer<TwitterUser> {
@Override
public JsonElement serialize(TwitterUser twitterUser, Type type, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.add("gid", context.serialize(twitterUser.getGid());
object.add("displayName", context.serialize(twitterUser.getDisplayName());
// ...
return object;
}
}
Then of course you need to pass this serializer to Gson during Setup like this:
Gson gson = new GsonBuilder().registerTypeAdapter(TwitterUser.class, new TwitterUserSerializer()).excludeFieldsWithoutExposeAnnotation().create();
String json = gson.toJson(twitterUser);
See also:
这篇关于Gson在android中的属性顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!