使用-GSON-android解析json数据

使用-GSON-android解析json数据,android,gson,Android,Gson,我得到以下回复: { "session_id":"3c59ba77-8545-4de7-aae0-41a5s1fads19", "user-info":{"username":"ganesh","website":null,"location":"newyork","bio":null,"ask":null,"name":"ganesh","isSelf":true} } 如何为此响应创建模型类: {

我得到以下回复:

          {
        "session_id":"3c59ba77-8545-4de7-aae0-41a5s1fads19",
        "user-info":{"username":"ganesh","website":null,"location":"newyork","bio":null,"ask":null,"name":"ganesh","isSelf":true}
         }
如何为此响应创建模型类:

          {
        "session_id":"3c59ba77-8545-4de7-aae0-41a5s1fads19",
        "user-info":{"username":"ganesh","website":null,"location":"newyork","bio":null,"ask":null,"name":"ganesh","isSelf":true}
         }
无法为用户信息创建对象名称

如何使用GSON解析此数据


您可以使用
FieldNamingPolicy

Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
然后,它将标准Java命名约定
userInfo
转换为
userInfo
(在Java中作为变量名是非法的),反之亦然

但在您的例子中,分隔符是混合的(一次是破折号,一次是下划线)

因此还有另一种解决方案,使用注释设置名称

import com.google.gson.annotations.SerializedName;

[..]

@SerializedName("user-info")
User_info userInfo;

用户信息
还是
用户信息
?将
-
更改为
.