使用批注使用谷歌GSON库Java模型类 [英] Use Annotation for Java model class using Google GSON library
问题描述
我有一个Android应用程序一个Java模型类。我需要这个类JSON格式的对象发送到C#Web服务(WCF)。
公共类版本{ 私人字符串核心;
私人字符串FastInspektion; 公共字符串getCore(){
返回核心;
} 公共无效setCore(串芯){
this.Core =核心;
} 公共字符串getFastInspektion(){
返回FastInspektion;
} 公共无效setFastInspektion(字符串fastInspektion){
this.FastInspektion = fastInspektion;
}
他们用大写字母在C#中第一个字母,但在Java中,我们使用小写。如何使用注释
在java中,以改变我的领域为小写,如核心,而不是核心等?
Json的创造者:
this.gson =新GsonBuilder()创建()。
OBJ =新版本();
obj.setCore(实验室实验室...);
obj.setFastInspektion(实验室实验室...);
gson.toJson(newobj);
通过GSON,你可以用注释 @SerializedName
你的领域给它一个不同的JSON名称。 (你说的类是在Java中,但你的字段名大写)。
通常
私人字符串核心;
将产生
{芯:一些价值}
相反,使用
@SerializedName(核心)
私人字符串核心;
产生
{核心:一些价值}
I have a Java model class in an android app. I need to send an object of this class in Json format to a Web Service in C# (WCF).
public class Version {
private String Core;
private String FastInspektion;
public String getCore() {
return Core;
}
public void setCore(String core) {
this.Core = core;
}
public String getFastInspektion() {
return FastInspektion;
}
public void setFastInspektion(String fastInspektion) {
this.FastInspektion = fastInspektion;
}
They are using uppercase for first letters in C#, but in java we use lowercase. How can I use Annotation
in java in order to change my fields to lowercase such as core instead of Core and etc?
Json creator:
this.gson = new GsonBuilder().create();
obj = new Version();
obj.setCore("lab lab...");
obj.setFastInspektion("lab lab...");
gson.toJson(newobj);
With Gson, you can annotate your field with @SerializedName
to give it a different JSON name. (You say the class is in Java, but your field names are capitalized.)
Typically
private String core;
would produce
{"core":"some value"}
Instead, use
@SerializedName("Core")
private String core;
which produces
{"Core":"some value"}
这篇关于使用批注使用谷歌GSON库Java模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!