Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否改变Model类里面的变量命名? #8

Open
kevin4z opened this issue Apr 29, 2016 · 2 comments
Open

能否改变Model类里面的变量命名? #8

kevin4z opened this issue Apr 29, 2016 · 2 comments

Comments

@kevin4z
Copy link

kevin4z commented Apr 29, 2016

Model类里的变量命名和返回JSON中的名字必须一样吗?现在如果命名不一样就不能正确解析JSON取到数据,请问有没有什么方法可以让两者命名可以不统一?比如JSON的description在代码里我只想命名为desc
Model里面命名:
public class ZhuangbiImage { public String description; public String image_url; }
返回JSON格式:
"id": 204, "description": "\u4e0d\u80fd\u56e0\u4e3a\u6211\u8d85\u53ef\u7231\u4f60\u5c31\u6b3a\u8d1f\u6211\u554a", ... ... "image_url": "http:\/\/7xjzdd.com1.z0.glb.clouddn.com\/i\/2015-07-31-4c92424a38c5db9c01abd63203584e1d.png"

@fg2q1q3q
Copy link

如果你自己来解析json比如Android自带的解析是可以这么做的,但如果你借助第三方比如gson,fastjson等恐怕不行

@TakWolf
Copy link

TakWolf commented Apr 29, 2016

字段别名,这个需求使用第三方JSON库也是支持的。
例如如果你使用Gson来解析,你可以这么定义:

   @SerializedName("description")
    private String desc;

fastjson也有类似的功能,请查阅对应库的文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants