android解析Json

android框架已经为我们集成了解析json的包

先一个简单的例子,json直接写在string中

Java代码

StringstaticObject="{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";

voidbuildObject()

{

try

{

obj=newJSONObject(staticObject);

Stringx=obj.get("firstname").toString()+""+obj.get("lastname").toString()+"has"+obj.getInt("cellphones")+"Androidphones.";

setStatus(x);

}

catch(JSONExceptionje)

{

setStatus("Erroroccured"+je.getMessage());

}

}

voidsetStatus(Stringx)

{

TextViewtv=(TextView)findViewById(R.id.txtStatus);

tv.setText(x);

}

StringstaticObject="{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";voidbuildObject(){try{obj=newJSONObject(staticObject);Stringx=obj.get("firstname").toString()+""+obj.get("lastname").toString()+"has"+obj.getInt("cellphones")+"Androidphones.";setStatus(x);}catch(JSONExceptionje){setStatus("Erroroccured"+je.getMessage());}}voidsetStatus(Stringx){TextViewtv=(TextView)findViewById(R.id.txtStatus);tv.setText(x);}

将json写在文件中,并放在raw目录下

json形式为

Java代码

{

"firstname":"Richard",

"lastname":"Stearns",

"almamater":"CornellUniversity",

"occupation":"President,WorldVision",

"interview":

{

"source":"http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY",

"questions":

[

{

"Question":"HowmuchmoneydoesWorldVisionraiseeveryyear?",

"Answer":"Worldwide,WorldVisionraisesabout$2billionannually;theU.S.office,whichIheadup,raisesabouthalfofthetotal."

},

{

"Question":"Isthisthe80/20rulewheretwentypercentofthepeoplesendineightypercentofthemoneyoraredonationsmorespreadout?",

"Answer":"WorldVision'sstrengthisthatwearesupportedbyhundredsofthousandsoffaithfulpeoplewhogiveusaboutadollaradaybysponsoringchildren.Our\"majordonors\"accountforlessthanfivepercentofourtotalincome.Also,foranon-profit,wehavequiteadiversifiedportfolioofrevenue.Justoverfortypercentiscashfromprivatecitizens;thirtypercentisgovernmentgrantsinfoodandcash;andaboutthirtypercentareproductsdonatedfromcorporation--whatwecall\"gifts-in-kind.\""

}

]

}

}

{"firstname":"Richard","lastname":"Stearns","almamater":"CornellUniversity","occupation":"President,WorldVision","interview":{"source":"http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY","questions":[{"Question":"HowmuchmoneydoesWorldVisionraiseeveryyear?","Answer":"Worldwide,WorldVisionraisesabout$2billionannually;theU.S.office,whichIheadup,raisesabouthalfofthetotal."},{"Question":"Isthisthe80/20rulewheretwentypercentofthepeoplesendineightypercentofthemoneyoraredonationsmorespreadout?","Answer":"WorldVision'sstrengthisthatwearesupportedbyhundredsofthousandsoffaithfulpeoplewhogiveusaboutadollaradaybysponsoringchildren.Our\"majordonors\"accountforlessthanfivepercentofourtotalincome.Also,foranon-profit,wehavequiteadiversifiedportfolioofrevenue.Justoverfortypercentiscashfromprivatecitizens;thirtypercentisgovernmentgrantsinfoodandcash;andaboutthirtypercentareproductsdonatedfromcorporation--whatwecall\"gifts-in-kind.\""}]}}

其中解析代码为

Java代码

voidbuildObjectFromFile()

{

try

{

Stringx="";

InputStreamis=this.getResources().openRawResource(R.raw.interview);

byte[]buffer=newbyte[is.available()];

while(is.read(buffer)!=-1);

Stringjson=newString(buffer);

obj=newJSONObject(json);

x=obj.getString("firstname")+""+obj.getString("lastname")+"n";

x+=obj.getString("occupation")+"n";

JSONObjectinterview=obj.getJSONObject("interview");

x+="Interviewsource:"+interview.getString("source")+"n";

JSONArrayquestions=interview.getJSONArray("questions");

x+="Numberofquestions:"+questions.length()+"nn";

inti;

for(i=0;i<questions.length();i++)

{

JSONObjectqa=questions.getJSONObject(i);

x+="------------n";

x+="Q"+(i+1)+"."+qa.getString("Question")+"nn";

x+="A"+(i+1)+"."+qa.getString("Answer")+"n";

}

setStatus(x);

}

catch(Exceptionje)

{

setStatus("Errorw/file:"+je.getMessage());

}

}

相关推荐