java json 嵌套

关于json嵌套解析5

Java代码   收藏代码
  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/ 

Java代码   收藏代码
  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<Address> emailaddrs = new ArrayList<Address>();  
  14.         private List<Address> telephones = new ArrayList<Address>();  
  15.         private List<Address> addresses = new ArrayList<Address>();  
  16.         private List<Address> urls = new ArrayList<Address>();  
  17.   
  18.         public String getFullname() {  
  19.             return fullname;  
  20.         }  
  21.   
  22.         public void setFullname(String fullname) {  
  23.             this.fullname = fullname;  
  24.         }  
  25.   
  26.         public String getOrg() {  
  27.             return org;  
  28.         }  
  29.   
  30.         public void setOrg(String org) {  
  31.             this.org = org;  
  32.         }  
  33.   
  34.         public List<Address> getEmailaddrs() {  
  35.             return emailaddrs;  
  36.         }  
  37.   
  38.         public void setEmailaddrs(List<Address> emailaddrs) {  
  39.             this.emailaddrs = emailaddrs;  
  40.         }  
  41.   
  42.         public List<Address> getTelephones() {  
  43.             return telephones;  
  44.         }  
  45.   
  46.         public void setTelephones(List<Address> telephones) {  
  47.             this.telephones = telephones;  
  48.         }  
  49.   
  50.         public List<Address> getAddresses() {  
  51.             return addresses;  
  52.         }  
  53.   
  54.         public void setAddresses(List<Address> addresses) {  
  55.             this.addresses = addresses;  
  56.         }  
  57.   
  58.         public List<Address> getUrls() {  
  59.             return urls;  
  60.         }  
  61.   
  62.         public void setUrls(List<Address> urls) {  
  63.             this.urls = urls;  
  64.         }  
  65.   
  66.     }  
  67.   
  68.     public static class Address {  
  69.         private String type = null;  
  70.         private String value = null;  
  71.         private String format = null;  
  72.         private int pref = 0;  
  73.   
  74.         public String getType() {  
  75.             return type;  
  76.         }  
  77.   
  78.         public void setType(String type) {  
  79.             this.type = type;  
  80.         }  
  81.   
  82.         public String getValue() {  
  83.             return value;  
  84.         }  
  85.   
  86.         public void setValue(String value) {  
  87.             this.value = value;  
  88.         }  
  89.   
  90.         public String getFormat() {  
  91.             return format;  
  92.         }  
  93.   
  94.         public void setFormat(String format) {  
  95.             this.format = format;  
  96.         }  
  97.   
  98.         public int getPref() {  
  99.             return pref;  
  100.         }  
  101.   
  102.         public void setPref(int pref) {  
  103.             this.pref = pref;  
  104.         }  
  105.   
  106.     }  
  107.   
  108.     public static void main(String[] args) throws Exception {  
  109.         ObjectMapper om = new ObjectMapper();  
  110.   
  111.         // 对象就在这里读取。  
  112.         Person person = om.readValue(System.in, Person.class);  
  113.   
  114.         // 怎么用,随便。  
  115.         System.out.println(person.getFullname());  
  116.         System.out.println(person.getEmailaddrs().get(0).getValue());  
  117.     }  
  118.   
  119. }  


http://www.iteye.com/problems/69856
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值