热门标签 | 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的实用性非常高。



推荐阅读
  • This pull request introduces the ability to provide comprehensive paragraph configurations directly within the Create Note and Create Paragraph REST endpoints, reducing the need for additional configuration calls. ... [详细]
  • 作为一名 Ember.js 新手,了解如何在路由和模型中正确加载 JSON 数据是至关重要的。本文将探讨两者之间的差异,并提供实用的建议。 ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文探讨了如何优化和正确配置Kafka Streams应用程序以确保准确的状态存储查询。通过调整配置参数和代码逻辑,可以有效解决数据不一致的问题。 ... [详细]
  • 本文详细介绍了 Java 中 org.apache.xmlbeans.SchemaType 类的 getBaseEnumType() 方法,提供了多个代码示例,并解释了其在不同场景下的使用方法。 ... [详细]
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • Composer Registry Manager:PHP的源切换管理工具
    本文介绍了一个用于Composer的源切换管理工具——Composer Registry Manager。该项目旨在简化Composer包源的管理和切换,避免与常见的CRM系统混淆,并提供了详细的安装和使用指南。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 本文详细介绍了在企业级项目中如何优化 Webpack 配置,特别是在 React 移动端项目中的最佳实践。涵盖资源压缩、代码分割、构建范围缩小、缓存机制以及性能优化等多个方面。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
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社区 版权所有