Parse JSON using gson with key name containing period (.)

风格不统一 提交于 2020-07-31 03:58:07

问题


{
   "data.url" : "http://dev.com",
   "value": [
       {
         "color": "red"
          "shape": "rect"
       },
       {
          "color": "blue"
          "shape": "rect"
       }
   ]
}

Tried using gson.json, but . character is blocking from creating class, Is there a way to remap the dotted field?


回答1:


Because Java doesn't allow . in a variable name, you need to use the @SerializedName annotation on that field in your class:

public class MyPojo {

    @SerializedName("data.url")
    private String dataUrl;
    ...

}


来源:https://stackoverflow.com/questions/21274183/parse-json-using-gson-with-key-name-containing-period

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!