使用在線工具jsonschema2pojo根據json生成java對象

jopen 9年前發布 | 20K 次閱讀 jsonschema2pojo

 

如果你還在自己手動寫model對象,那你就out了。

場景:使用retrofit請求github的api,要使返回的數據自動解析到java對象中你必須準備好一個和json完全對應的java對象。而github的一個Repository有很多字段,如這個請求:

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

你可以直接打開這個鏈接查看返回的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": "git@github.com: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/ 網站左邊的輸入框中,右邊的設置請按照圖里的來。 

使用在線工具jsonschema2pojo根據json生成java對象

其中右邊設置的Class name一項默認是Example,表示生成的對象名(最外層)就是Example,因為它無法從json中得知這到底是什么對象。我們這里期望得到的對 象名是Repository,可以改成Repository就可以了,或者這里不改,在生成之后的java對象里改。

注意里面還有個owner,它本身也是對象,jsonschema2pojo能把這個對象也解析出來,而這個對象的類名耶就是 Owner

我們點擊Preview按鈕,在彈出的對話框中生成了兩個類:

使用在線工具jsonschema2pojo根據json生成java對象

第一個就是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?=desc

使用在線工具jsonschema2pojo根據json生成java對象

返回的json數據首先打印的是列表長度total_count,這里是297。這是搜索結果的總長度,但是其實這次請求返回的只有30 條,因為這是第一頁的數據。但是30條已經很長了,超過了長度限制。

要把這樣的json轉換成對象必須得找個搜索結果比較短的,我們換成

https://api.github.com/search/repositories?q=codebox+language:java&sort=stars?=desc

這個返回的結果只有9項,雖然也很長但是還是沒有超過限制。

使用在線工具jsonschema2pojo根據json生成java對象

直接把生成的結果復制到jsonschema2pojo的左側輸入框,把類名改成Repositories,表示這是一個數據集合。

生成的類結構從前面部分就能看出來了,分別是Repositories,items以及Owner,其中items對應的就是剛剛我們的Repository類,這里它只能根據json猜測這個類名是items。然而Class name設置的只是最外層的類名。

我們可以生成之后把items改成Repository就行了。或者有個技巧,把json的items直接改成Repository。

我之所以再舉一個搜索結果為列表的例子是為了驗證一個列表式的json是否可以直接轉換成java類。

總結

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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!