如果你还在自己手动写model对象,那你就out了。
场景:使用retrofit请求github的api,要使返回的数据自动解析到java对象中你必须准备好一个和json完全对应的java对象。而github的一个Repository有很多字段,如这个请求:
你可以直接打开这个链接查看返回的json数据。可以看到里面有很多数据项,如果我们手动写这个java对象将非常耗费时间。
所幸现在有了转换的工具。
jsonschema2pojo
jsonschema2pojo 是一个根据json转换成java对象的开源项目,只要把你的json字符串复制到相应的输入框中就能自动将其转换成java对象。它的强大之处在于,能解析列表式的json数据,把嵌套在内层的对象也解析出来。
先以上面的api请求为例,得到的json如下,这是一个单一的Repository数据:{
"id": 1296269,
"name": "Hello-World",
"full_name": "octocat/Hello-World",
"owner": {
"login": "octocat",
"id": 583231,
"avatar_url": "https://avatars.githubusercontent.com/u/583231?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin"