2 qq 15173955 qq_15173955 于 2016.04.12 11:03 提问

jackson1.7.6读取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 java.util.ArrayList;
import java.util.List;

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;
    private GlossDef glossDef=null;
    private GlossDiv glossDiv=null;
    private GlossEntry glossEntry=null;
    private GlossList glossList=null;


    @Before
    public void init(){
        glossary=new Glossary();
        glossary.setTitle("example glossary");



#       **此处需补充(jackson版本为1.7.6,补充代码为去读json,json在最下面附上)**



        objectMapper =new ObjectMapper();
        try{
            jsonGenerator=objectMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    @Test
    public void readJson2Entity() {
        try {
            Glossary glo = objectMapper.readValue(new File("d:" + File.separator + "a2.json"), Glossary.class);
            System.out.println(glo.getTitle());
            System.out.println(glo.getGlossDiv().getTitle());
            System.out.println(glo.getGlossDiv().getGlossList());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getID());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getSortAs());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getGlossTerm());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getAcronym());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getAbbrev());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getGlossDef());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getGlossDef().getPara());
            System.out.println(glo.getGlossDiv().getGlossList().getGlossEntry().getGlossSee());
            System.out.println(glo);
        } 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;
            glossary=null;
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

{
"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"
}
}
}
}
}

1个回答

xionglangs
xionglangs   Rxr 2016.04.12 11:24

}下一个 jackson-all-1.7.6.jar

public static T mapperObject(String content, TypeReference typeRef) {
ObjectMapper mapper = new ObjectMapper();
//mapper.enableDefaultTyping();
try {
return mapper.readValue(content, typeRef);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

List<对象> 变量名= JacksonUtils.mapperObject(json数据,new TypeReference>() {
});
对象的属性为1,2,3,4;属性的值为100,200,300,400

qq_15173955
qq_15173955 回复编程爱好者熊浪: jackson1.7.6下好了,现在需要读出来json里的东西,不会做
一年多之前 回复
xionglangs
xionglangs 看下这个。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!