热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用在线工具jsonschema2pojo根据json生成java对象

本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。

如果你还在自己手动写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": false

},

"private": false,

"html_url": "https://github.com/octocat/Hello-World",

"description": "This your first repo!",

"fork": false,

"url": "https://api.github.com/repos/octocat/Hello-World",

"forks_url": "https://api.github.com/repos/octocat/Hello-World/forks",

"keys_url": "https://api.github.com/repos/octocat/Hello-World/keys{/key_id}",

"collaborators_url": "https://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}",

"teams_url": "https://api.github.com/repos/octocat/Hello-World/teams",

"hooks_url": "https://api.github.com/repos/octocat/Hello-World/hooks",

"issue_events_url": "https://api.github.com/repos/octocat/Hello-World/issues/events{/number}",

"events_url": "https://api.github.com/repos/octocat/Hello-World/events",

"assignees_url": "https://api.github.com/repos/octocat/Hello-World/assignees{/user}",

"branches_url": "https://api.github.com/repos/octocat/Hello-World/branches{/branch}",

"tags_url": "https://api.github.com/repos/octocat/Hello-World/tags",

"blobs_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}",

"git_tags_url": "https://api.github.com/repos/octocat/Hello-World/git/tags{/sha}",

"git_refs_url": "https://api.github.com/repos/octocat/Hello-World/git/refs{/sha}",

"trees_url": "https://api.github.com/repos/octocat/Hello-World/git/trees{/sha}",

"statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/{sha}",

"languages_url": "https://api.github.com/repos/octocat/Hello-World/languages",

"stargazers_url": "https://api.github.com/repos/octocat/Hello-World/stargazers",

"contributors_url": "https://api.github.com/repos/octocat/Hello-World/contributors",

"subscribers_url": "https://api.github.com/repos/octocat/Hello-World/subscribers",

"subscription_url": "https://api.github.com/repos/octocat/Hello-World/subscription",

"commits_url": "https://api.github.com/repos/octocat/Hello-World/commits{/sha}",

"git_commits_url": "https://api.github.com/repos/octocat/Hello-World/git/commits{/sha}",

"comments_url": "https://api.github.com/repos/octocat/Hello-World/comments{/number}",

"issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments{/number}",

"contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/{+path}",

"compare_url": "https://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}",

"merges_url": "https://api.github.com/repos/octocat/Hello-World/merges",

"archive_url": "https://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}",

"downloads_url": "https://api.github.com/repos/octocat/Hello-World/downloads",

"issues_url": "https://api.github.com/repos/octocat/Hello-World/issues{/number}",

"pulls_url": "https://api.github.com/repos/octocat/Hello-World/pulls{/number}",

"milestones_url": "https://api.github.com/repos/octocat/Hello-World/milestones{/number}",

"notifications_url": "https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}",

"labels_url": "https://api.github.com/repos/octocat/Hello-World/labels{/name}",

"releases_url": "https://api.github.com/repos/octocat/Hello-World/releases{/id}",

"created_at": "2011-01-26T19:01:12Z",

"updated_at": "2015-10-08T07:23:02Z",

"pushed_at": "2015-10-02T01:04:21Z",

"git_url": "git://github.com/octocat/Hello-World.git",

"ssh_url": "[email protected]:octocat/Hello-World.git",

"clone_url": "https://github.com/octocat/Hello-World.git",

"svn_url": "https://github.com/octocat/Hello-World",

"homepage": "",

"size": 566,

"stargazers_count": 1402,

"watchers_count": 1402,

"language": null,

"has_issues": true,

"has_downloads": true,

"has_wiki": true,

"has_pages": false,

"forks_count": 1110,

"mirror_url": null,

"open_issues_count": 139,

"forks": 1110,

"open_issues": 139,

"watchers": 1402,

"default_branch": "master",

"network_count": 1110,

"subscribers_count": 1747

}

其中有一个数据项比较特殊,那就是owner(在第5行),它的值也是一个对象。我们把这个json放在http://www.jsonschema2pojo.org/ 网站左边的输入框中,右边的设置请按照图里的来。

1444873936917437.png

其中右边设置的Class name一项默认是Example,表示生成的对象名(最外层)就是Example,因为它无法从json中得知这到底是什么对象。我们这里期望得到的对象名是Repository,可以改成Repository就可以了,或者这里不改,在生成之后的java对象里改。

注意里面还有个owner,它本身也是对象,jsonschema2pojo能把这个对象也解析出来,而这个对象的类名耶就是Owner。

我们点击Preview按钮,在弹出的对话框中生成了两个类:

1444874333434965.png

第一个就是Owner,第二个截图没有显示出来,是Repository类(我把Class name改成了Repository)。

而这里的@Generated("org.jsonschema2pojo")可以直接删掉。

可以想象如此之多的变量如果自己写要花费多长时间。

不过需要注意的是jsonschema2pojo对json字符串的总长度有要求。这样如果你像比如github请求的是一个列表数据比如,那么你得到的json很可能超过长度限制。

比如我们使用github的搜索api发出一个这样的请求:https://api.github.com/search/repositories?q=tetris+language:assembly&sort=stars&order=desc

1444874793465130.png

返回的json数据首先打印的是列表长度total_count,这里是297。这是搜索结果的总长度,但是其实这次请求返回的只有30 条,因为这是第一页的数据。但是30条已经很长了,超过了长度限制。

要把这样的json转换成对象必须得找个搜索结果比较短的,我们换成https://api.github.com/search/repositories?q=codebox+language:java&sort=stars&order=desc

这个返回的结果只有9项,虽然也很长但是还是没有超过限制。

1444875495130089.png

直接把生成的结果复制到jsonschema2pojo的左侧输入框,把类名改成Repositories,表示这是一个数据集合。

生成的类结构从前面部分就能看出来了,分别是Repositories,Item以及Owner,其中Item对应的就是刚刚我们的Repository类,这里它只能根据json猜测这个类名是Item。

我们可以生成之后把Item改成Repository就行了。

我之所以再举一个搜索结果为列表的例子是为了验证一个列表式的json是否可以直接转换成java类。

注意

以上只是生成普通的对象,但是如果想让生成的类和retrofit一起使用并直接返回解析好了的Repositories对象,里面的类名保留为默认的才行。而且retrofit默认使用的是GsonConverter,因此在解析之前,最好把Annotation style设置成Gson。

1444961322463021.png

使用这种方式生成的java类会用注解的方式把json中的字段和类中的字段对应:@SerializedName("full_name")

@Expose

private String fullName;

这表示在json中字段名为full_name而在类中变量名为fullName。

这两个注解是gson的因此import com.google.gson.annotations.Expose;

import com.google.gson.annotations.SerializedName;

总结

可以看到jsonschema2pojo的实用性非常高。



推荐阅读
  • 如何去除Win7快捷方式的箭头
    本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ... [详细]
  • 抽空写了一个ICON图标的转换程序
    抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换。具体的程序就在下面,如果看的人多,过两天再把思路写一下。 ... [详细]
  • 建立的索引后,select语句中,where条件和排序条件必须完全和索引一样,索引才能真正起到作用,并且排序条件的先后顺序必须和索引一致.where的字段必须处于索引最开始的位置, ... [详细]
  • SAP羞辱国产软件商:技术停在10年前
    SAP中国研究院总裁芮祥麟表示,国产软件厂商过于热衷概念炒作,技术水平停留在10年前的客户端架构水平。他认为,国内厂商推出基于SOA的产品或转型SAAS模式是不可能的,研发新架构需要时间。当前最热门的概念是云计算,芮祥麟呼吁国产厂商应该潜心研发底层架构。 ... [详细]
  • IT方面的论坛太多了,有综合,有专业,有行业,在各个论坛里混了几年,体会颇深,以前是论坛哪里人多 ... [详细]
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 本文讲述了孙悟空写给白骨精的信件引发的思考和反省。孙悟空在信中对自己的行为进行了反思,认识到自己胡闹的行为并没有给他带来实际的收获。他也揭示了西天取经的真相,认为这是玉皇、菩萨设下的一场陷阱。他还提到了师傅的虚伪和对自己的实心话,以及自己作为师傅准备提拔的对象而被派下来锻炼的经历。他认为路上的九九八十一难也都是菩萨算计好的,唐僧并没有真正的危险。最后,他提到了观音菩萨在关键时刻的指导。这封信件引发了孙悟空对自己行为的思考和反省,对西天取经的目的和自己的角色有了更深入的认识。 ... [详细]
  • Windows2003 IIS上设置301定向,实现不带www域名跳转带www域名的方法
    打开IIS,建一个网站,主机头用不带www的域名,随便指向一个目录。然后在这个网站上点右键,属性--主目录--重定向到URL如图ÿ ... [详细]
  • REVERT权限切换的操作步骤和注意事项
    本文介绍了在SQL Server中进行REVERT权限切换的操作步骤和注意事项。首先登录到SQL Server,其中包括一个具有很小权限的普通用户和一个系统管理员角色中的成员。然后通过添加Windows登录到SQL Server,并将其添加到AdventureWorks数据库中的用户列表中。最后通过REVERT命令切换权限。在操作过程中需要注意的是,确保登录名和数据库名的正确性,并遵循安全措施,以防止权限泄露和数据损坏。 ... [详细]
  • 本文详细介绍了使用 SQL Load 和 Excel 的 Concatenate 功能将数据导入 ORACLE 数据库的方法和步骤,同时介绍了使用 PL/SQL tools 将数据导入临时表的方法。此外,还提供了一个转链接,可参考更多相关内容。摘要共计XXX字。 ... [详细]
  • 本文分析了Wince程序内存和存储内存的分布及作用。Wince内存包括系统内存、对象存储和程序内存,其中系统内存占用了一部分SDRAM,而剩下的30M为程序内存和存储内存。对象存储是嵌入式wince操作系统中的一个新概念,常用于消费电子设备中。此外,文章还介绍了主电源和后备电池在操作系统中的作用。 ... [详细]
  • 在搜索数据库中的数据时,您可以使用SQL通配符。SQL通配符在搜索数据库中的数据时,SQL通配符可以替代一个或多个字符。SQL通配符必须与LIKE运算符 ... [详细]
  • https:github.comdarcyclarkeFront-end-Developer-Interview-QuestionstreemasterChinese#refere ... [详细]
  • 由于同源策略的限制,满足同源的脚本才可以获取资源。虽然这样有助于保障网络安全,但另一方面也限制了资源的使用。那么如何实现跨域呢,以下是实现跨域的一些方法。 ... [详细]
  • 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?Input测 ... [详细]
author-avatar
tony2502877947
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有