{
"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(); } } }
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");
新手建议自己手动解析,熟练之后再去用一些解析框架.
没看懂
@你的月亮我的心cy: 这个json对象里面有对象嵌套多个,它的getset方法我都做完了.现在要把它打印到控制台上,用jackson1.7.6
@你的月亮我的心cy: 昨天代码发乱了,我重新发了,帮我看看
http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html
那个看过了,我这是多层,我不会做
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; } }