首页 新闻 搜索 专区 学院

用jackson1.7.6读取json

0
悬赏园豆:60 [已解决问题] 解决于 2016-04-13 11:15
{
    "glossary": {
        "title": "example glossary", 
        "GlossDiv": {
            "title": "S", 
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML", 
                    "SortAs": "SGML", 
                    "GlossTerm": "Standard Generalized Markup Language", 
                    "Acronym": "SGML", 
                    "Abbrev": "ISO 8879:1986", 
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook."
                    }, 
                    "GlossSee": "markup"
                }
            }
        }
    }
}

需要获取到这个json的所有字符串并打印在控制台,所有的分献上

问题补充:
package per.sww.eleven_five;

public class Glossary {
    private String title;
    private GlossDiv GlossDiv;
    
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public GlossDiv getGlossDiv() {
        return GlossDiv;
    }
    public void setGlossDiv(GlossDiv glossDiv) {
        GlossDiv = glossDiv;
    }
}
package per.sww.eleven_five;

public class GlossDef {
    
    public GlossDef(String para) {
        this.para=para;
    }
    
    private String para;

    public String getPara() {
        return para;
    }

    public void setPara(String para) {
        this.para = para;
    }
}
package per.sww.eleven_five;

public class GlossDiv {
    
    
    private String title;
    private GlossList GlossList;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public GlossList getGlossList() {
        return GlossList;
    }
    public void setGlossList(GlossList glossList) {
        GlossList = glossList;
    }
}
package per.sww.eleven_five;

public class GlossEntry {
    private String ID;
    private String SortAs;
    private String GlossTerm;
    private String Acronym;
    private String Abbrev;
    private GlossDef GlossDef;
    private String GlossSee;
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    public String getSortAs() {
        return SortAs;
    }
    public void setSortAs(String sortAs) {
        SortAs = sortAs;
    }
    public String getGlossTerm() {
        return GlossTerm;
    }
    public void setGlossTerm(String glossTerm) {
        GlossTerm = glossTerm;
    }
    public String getAcronym() {
        return Acronym;
    }
    public void setAcronym(String acronym) {
        Acronym = acronym;
    }
    public String getAbbrev() {
        return Abbrev;
    }
    public void setAbbrev(String abbrev) {
        Abbrev = abbrev;
    }
    public GlossDef getGlossDef() {
        return GlossDef;
    }
    public void setGlossDef(GlossDef glossDef) {
        GlossDef = glossDef;
    }
    public String getGlossSee() {
        return GlossSee;
    }
    public void setGlossSee(String glossSee) {
        GlossSee = glossSee;
    }
}
package per.sww.eleven_five;

public class GlossList {
    
    private GlossEntry GlossEntry;

    public GlossEntry getGlossEntry() {
        return GlossEntry;
    }

    public void setGlossEntry(GlossEntry glossEntry) {
        GlossEntry = glossEntry;
    }
    
}
package per.sww.eleven_five;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JasksonBean {
    private JsonGenerator jsonGenerator=null;
    private ObjectMapper objectMapper=null;
    private Glossary glossary=null;
    
    
    @Before
    public void init(){
        glossary=new Glossary();
        glossary.setTitle("example glossary");
        
        
        
        
        //此处需代码
        
        
        
        
        objectMapper =new ObjectMapper();
        try{
            jsonGenerator=objectMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    @Test
    public void jaskTest(){
         try {
             Glossary acc = objectMapper.readValue(new File("d:" + File.separator + "a2.json"), Glossary.class);
                System.out.println();
                System.out.println(acc);
            } catch (JsonParseException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
        }
    }
    
    @After
    public void putAfter(){
        try{
            if(jsonGenerator!=null){
                jsonGenerator.flush();
            }
            if(!jsonGenerator.isClosed()){
                jsonGenerator.close();;
            }
            jsonGenerator=null;
            objectMapper=null;
            
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

 

师太来老道这坐坐的主页 师太来老道这坐坐 | 初学一级 | 园豆:34
提问于:2016-04-11 17:51
< >
分享
最佳答案
0
     JSONObject obj=new JSONObject("你这段json");
        JSONObject glossary=obj.getJSONObject("glossary");
        String title=glossary.getString("title");
        JSONObject GlossDiv=glossary.getJSONObject("GlossDiv");
        String title_02=GlossDiv.getString("title");
        JSONObject GlossList=GlossDiv.getJSONObject("GlossList");
        JSONObject GlossEntry=GlossList.getJSONObject("GlossEntry");
        String ID=GlossEntry.getString("ID");
        String SortAs=GlossEntry.getString("SortAs");
        String GlossTerm=GlossEntry.getString("GlossTerm");
        String Acronym=GlossEntry.getString("Acronym");
        String Abbrev=GlossEntry.getString("Abbrev");
        String GlossSee=GlossEntry.getString("GlossSee");
        JSONObject GlossDef=GlossEntry.getJSONObject("GlossDef");
        String para=GlossDef.getString("para");

 

收获园豆:60
你的月亮我的心cy | 菜鸟二级 |园豆:243 | 2016-04-11 18:09

新手建议自己手动解析,熟练之后再去用一些解析框架. 

你的月亮我的心cy | 园豆:243 (菜鸟二级) | 2016-04-11 18:10

没看懂

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-12 09:18

@你的月亮我的心cy: 这个json对象里面有对象嵌套多个,它的getset方法我都做完了.现在要把它打印到控制台上,用jackson1.7.6

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-12 09:22

@你的月亮我的心cy: 昨天代码发乱了,我重新发了,帮我看看

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-12 09:25
其他回答(2)
0

http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html

✎﹏ℳ๓₯㎕ღ | 园豆:1497 (小虾三级) | 2016-04-11 17:57

那个看过了,我这是多层,我不会做

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-11 17:58
0
package per.sww.eleven_sex;

public class TestJson {
    private Glossary glossary = null;

    public Glossary getGlossary() {
        return glossary;
    }

    public void setGlossary(Glossary glossary) {
        this.glossary = glossary;
    }
}

 

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-13 10:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册