使用Jackson框架解析Json示例

首先需要下载包:jackson-databind-2.0.5.jar jackson-annotations-2.0.5.jar jackson-core-2.0.5.jar,网址在:http://wiki.fasterxml.com/JacksonDownload 

 

custom.json:

{
    "country":"USA",   
    "countryInfo":{"golden":40,"silver":23,"copper":33,"tank":1},  
"countryDetail":[  
    {"id":"no","header":"号码","fieldIndex":"no","sortOrder":null,"printable":true},  
    {"id":"name","header":"姓名","fieldIndex":"name","sortOrder":null,"printable":true},  
    {"id":"total","header":"总成绩","fieldIndex":"total","sortOrder":null,"printable":true},  
    {"id":"detail","header":"详细信息","fieldIndex":"detail","sortOrder":null,"printable":true}  
   ], 

"orgood":true}  


Java的Bean及解析: 

import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class Bean{
    
    private String country;
    private boolean orgood;
    private Country countryInfo;
    private CountryDetail[] countryDetail;
    
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public boolean isOrgood() {
        return orgood;
    }

    public void setOrgood(boolean orgood) {
        this.orgood = orgood;
    }

    public Country getCountryInfo() {
        return countryInfo;
    }

    public void setCountryInfo(Country countryInfo) {
        this.countryInfo = countryInfo;
    }

    public CountryDetail[] getCountryDetail() {
        return countryDetail;
    }

    public void setCountryDetail(CountryDetail[] countryDetail) {
        this.countryDetail = countryDetail;
    }
    
    public static class Country{
        public String getGolden() {
            return golden;
        }
        public void setGolden(String golden) {
            this.golden = golden;
        }
        public String getSilver() {
            return silver;
        }
        public void setSilver(String silver) {
            this.silver = silver;
        }
        public String getCopper() {
            return copper;
        }
        public void setCopper(String copper) {
            this.copper = copper;
        }
        public String getTank() {
            return tank;
        }
        public void setTank(String tank) {
            this.tank = tank;
        }
        private String golden;
        private String silver;
        private String copper;
        private String tank;
        
    }
    
    public static class CountryDetail{
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getHeader() {
            return header;
        }
        public void setHeader(String header) {
            this.header = header;
        }
        public String getFieldIndex() {
            return fieldIndex;
        }
        public void setFieldIndex(String fieldIndex) {
            this.fieldIndex = fieldIndex;
        }
        public String getSortOrder() {
            return sortOrder;
        }
        public void setSortOrder(String sortOrder) {
            this.sortOrder = sortOrder;
        }
        public boolean isPrintable() {
            return printable;
        }
        public void setPrintable(boolean printable) {
            this.printable = printable;
        }
        private String id;
        private String header;
        private String fieldIndex;
        private String sortOrder;
        private boolean printable;
        
    }
    
    public static void main(String args[]){
        ObjectMapper mapper = new ObjectMapper();
        try {
            Bean bean = mapper.readValue(new File("custom.json"), Bean.class);

    //读取 

            System.out.println("Country:"+bean.getCountry());
            System.out.println("CountryInfo:"+bean.getCountryInfo().getGolden());
            System.out.println("CountryDetail:"+bean.getCountryDetail()[1].getHeader());
            System.out.println("orgood:"+bean.isOrgood());
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

} 


 

posted on 2012-08-23 16:59  lee0oo0  阅读(34363)  评论(9编辑  收藏  举报