mirinda95925 2011-08-11 14:40
浏览 375
已采纳

关于json嵌套解析

[code="java"]
{

"fullname": "Sean Kelly",

"org": "SK Consulting",

"emailaddrs": [

{"type": "work", "value": "kelly@seankelly.biz"},

{"type": "home", "pref": 1, "value": "kelly@seankelly.tv"}

],

"telephones": [

{"type": "work", "pref": 1, "value": "+1 214 555 1212"},

{"type": "fax", "value": "+1 214 555 1213"},

{"type": "mobile", "value": "+1 214 555 1214"}

],

"addresses": [

{"type": "work", "format": "us",

"value": "1234 Main StnSpringfield, TX 78080-1216"},

{"type": "home", "format": "us",

"value": "5678 Main StnSpringfield, TX 78080-1316"}

],

"urls": [

{"type": "work", "value": "http://seankelly.biz/"},

{"type": "home", "value": "http://seankelly.tv/"}

]

}

[/code]
请问如何取每个元素啊~来段能用的代码,谢谢~

  • 写回答

5条回答 默认 最新

  • wks1986 2011-08-11 15:01
    关注

    需要Jackson。[url]http://jackson.codehaus.org/[/url]

    [code="java"]package jsonparsingtest;

    import java.util.ArrayList;
    import java.util.List;

    import org.codehaus.jackson.map.ObjectMapper;

    public class JsonParsingTest {

    public static class Person {
        private String fullname = null;
        private String org = null;
        private List<Address> emailaddrs = new ArrayList<Address>();
        private List<Address> telephones = new ArrayList<Address>();
        private List<Address> addresses = new ArrayList<Address>();
        private List<Address> urls = new ArrayList<Address>();
    
        public String getFullname() {
            return fullname;
        }
    
        public void setFullname(String fullname) {
            this.fullname = fullname;
        }
    
        public String getOrg() {
            return org;
        }
    
        public void setOrg(String org) {
            this.org = org;
        }
    
        public List<Address> getEmailaddrs() {
            return emailaddrs;
        }
    
        public void setEmailaddrs(List<Address> emailaddrs) {
            this.emailaddrs = emailaddrs;
        }
    
        public List<Address> getTelephones() {
            return telephones;
        }
    
        public void setTelephones(List<Address> telephones) {
            this.telephones = telephones;
        }
    
        public List<Address> getAddresses() {
            return addresses;
        }
    
        public void setAddresses(List<Address> addresses) {
            this.addresses = addresses;
        }
    
        public List<Address> getUrls() {
            return urls;
        }
    
        public void setUrls(List<Address> urls) {
            this.urls = urls;
        }
    
    }
    
    public static class Address {
        private String type = null;
        private String value = null;
        private String format = null;
        private int pref = 0;
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    
        public String getFormat() {
            return format;
        }
    
        public void setFormat(String format) {
            this.format = format;
        }
    
        public int getPref() {
            return pref;
        }
    
        public void setPref(int pref) {
            this.pref = pref;
        }
    
    }
    
    public static void main(String[] args) throws Exception {
        ObjectMapper om = new ObjectMapper();
    
        // 对象就在这里读取。
        Person person = om.readValue(System.in, Person.class);
    
        // 怎么用,随便。
        System.out.println(person.getFullname());
        System.out.println(person.getEmailaddrs().get(0).getValue());
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程