关于json嵌套解析

猫哥 程序代码评论字数 2292阅读7分38秒阅读模式

转载自 http://www.iteye.com/problems/69856

  1. {
  2. "fullname": "Sean Kelly",
  3. "org": "SK Consulting",
  4. "emailaddrs": [
  5. {"type": "work", "value": "kelly@seankelly.biz"},
  6. {"type": "home", "pref": 1, "value": "kelly@seankelly.tv"}
  7. ],
  8. "telephones": [
  9. {"type": "work", "pref": 1, "value": "+1 214 555 1212"},
  10. {"type": "fax", "value": "+1 214 555 1213"},
  11. {"type": "mobile", "value": "+1 214 555 1214"}
  12. ],
  13. "addresses": [
  14. {"type": "work", "format": "us",
  15. "value": "1234 Main StnSpringfield, TX 78080-1216"},
  16. {"type": "home", "format": "us",
  17. "value": "5678 Main StnSpringfield, TX 78080-1316"}
  18. ],
  19. "urls": [
  20. {"type": "work", "value": "http://seankelly.biz/"},
  21. {"type": "home", "value": "http://seankelly.tv/"}
  22. ]
  23. }

需要Jackson。http://jackson.codehaus.org/

  1. package jsonparsingtest;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.codehaus.jackson.map.ObjectMapper;
  7.  
  8. public class JsonParsingTest {
  9.  
  10. public static class Person {
  11. private String fullname = null;
  12. private String org = null;
  13. private List
    emailaddrs = new ArrayList
    ();
  14. private List
    telephones = new ArrayList
    ();
  15. private List
    addresses = new ArrayList
    ();
  16. private List
    urls = new ArrayList
    ();
  17. public String getFullname() {
  18. return fullname;
  19. }
  20. public void setFullname(String fullname) {
  21. this.fullname = fullname;
  22. }
  23. public String getOrg() {
  24. return org;
  25. }
  26. public void setOrg(String org) {
  27. this.org = org;
  28. }
  29. public List
    getEmailaddrs() {
  30. return emailaddrs;
  31. }
  32. public void setEmailaddrs(List
    emailaddrs) {
  33. this.emailaddrs = emailaddrs;
  34. }
  35. public List
    getTelephones() {
  36. return telephones;
  37. }
  38. public void setTelephones(List
    telephones) {
  39. this.telephones = telephones;
  40. }
  41. public List
    getAddresses() {
  42. return addresses;
  43. }
  44. public void setAddresses(List
    addresses) {
  45. this.addresses = addresses;
  46. }
  47. public List
    getUrls() {
  48. return urls;
  49. }
  50. public void setUrls(List
    urls) {
  51. this.urls = urls;
  52. }
  53. }
  54. public static class Address {
  55. private String type = null;
  56. private String value = null;
  57. private String format = null;
  58. private int pref = 0;
  59. public String getType() {
  60. return type;
  61. }
  62. public void setType(String type) {
  63. this.type = type;
  64. }
  65. public String getValue() {
  66. return value;
  67. }
  68. public void setValue(String value) {
  69. this.value = value;
  70. }
  71. public String getFormat() {
  72. return format;
  73. }
  74. public void setFormat(String format) {
  75. this.format = format;
  76. }
  77. public int getPref() {
  78. return pref;
  79. }
  80. public void setPref(int pref) {
  81. this.pref = pref;
  82. }
  83. }
  84. public static void main(String[] args) throws Exception {
  85. ObjectMapper om = new ObjectMapper();
  86. // 对象就在这里读取。
  87. Person person = om.readValue(System.in, Person.class);
  88. // 怎么用,随便。
  89. System.out.println(person.getFullname());
  90. System.out.println(person.getEmailaddrs().get(0).getValue());
  91. }
  92. }

继续阅读
猫哥
  • 本文由 发表于 2015-01-20 22:14:07
  • 转载请务必保留本文链接:https://www.588693.com/205.html
安卓通过shell命令启动APP怎么操作 程序代码

安卓通过shell命令启动APP怎么操作

要启动Android应用程序,可以使用adb shell命令,语法如下:adb shell am start -n /。其中,是应用程序的包名,是应用程序的入口Activity名称。 相关问题 and...
Android build.prop简介 程序代码

Android build.prop简介

build.prop位于手机的/system/build.prop中 build.prop记录一些系统设置,是一个属性文件,相当于Windows系统的注册表。 build.prop生成: Make系统...
php程序列表程序 程序代码

php程序列表程序

记录备份一下 H5ai,就是一个文件目录列表程序,可以搭建在服务器上,可以作为下载站点,也可以当成私有云盘。h5ai是一款功能强大 php 文件目录列表程序,不需要数据库,支持在线预览文本、图片、音频...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定