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


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

Java代码
  1. String staticObject = "{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";  
  2.   
  3. void buildObject()  
  4. {  
  5.     try  
  6.     {  
  7.         obj = new JSONObject(staticObject);  
  8.         String x = obj.get("firstname").toString() + " " + obj.get("lastname").toString() + " has " + obj.getInt("cellphones") + " Android phones.";  
  9.         setStatus(x);  
  10.     }  
  11.     catch (JSONException je)  
  12.     {  
  13.         setStatus("Error occured " + je.getMessage());  
  14.     }  
  15. }  
  16.   
  17. void setStatus(String x)  
  18. {  
  19.     TextView tv = (TextView) findViewById(R.id.txtStatus);  
  20.     tv.setText(x);  
  21. }  



将json写在文件中,并放在raw目录下 
json形式为 
Java代码
  1. {  
  2. "firstname":"Richard",  
  3. "lastname":"Stearns",  
  4. "almamater":"Cornell University",  
  5. "occupation":"President, World Vision",  
  6. "interview":  
  7.     {  
  8.         "source" : "http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY",  
  9.         "questions":  
  10.             [  
  11.             {  
  12.             "Question""How much money does World Vision raise every year?",  
  13.             "Answer""Worldwide, World Vision raises about $2 billion annually; the U.S. office, which I head up, raises about half of the total."  
  14.             },  
  15.             {  
  16.             "Question""Is this the 80/20 rule where twenty percent of the people send in eighty percent of the money or are donations more spread out?",  
  17.             "Answer""World Vision's strength is that we are supported by hundreds of thousands of faithful people who give us about a dollar a day by sponsoring children. Our \"major donors\" account for less than five percent of our total income. Also, for a non-profit, we have quite a diversified portfolio of revenue. Just over forty percent is cash from private citizens; thirty percent is government grants in food and cash; and about thirty percent are products donated from corporation--what we call \"gifts-in-kind.\""  
  18.             }  
  19.             ]  
  20.     }  
  21. }  



其中解析代码为 
Java代码
  1. void buildObjectFromFile()  
  2. {  
  3.     try  
  4.     {  
  5.         String x = "";  
  6.         InputStream is = this.getResources().openRawResource(R.raw.interview);  
  7.         byte [] buffer = new byte[is.available()];  
  8.         while (is.read(buffer) != -1);  
  9.         String json = new String(buffer);  
  10.         obj = new JSONObject(json);  
  11.         x = obj.getString("firstname") + " " + obj.getString("lastname") + "n";  
  12.         x += obj.getString("occupation") + "n";  
  13.   
  14.         JSONObject interview =  obj.getJSONObject("interview");  
  15.         x += "Interview source:" + interview.getString("source")  + "n";  
  16.   
  17.         JSONArray questions = interview.getJSONArray("questions");  
  18.         x += "Number of questions: " + questions.length()  + "nn";  
  19.   
  20.         int i;  
  21.         for (i=0;i<questions.length();i++)  
  22.         {  
  23.             JSONObject qa = questions.getJSONObject(i);  
  24.             x += "------------n";  
  25.             x += "Q" + (i+1) + ". " + qa.getString("Question") + "nn";  
  26.             x += "A" + (i+1) + ". " + qa.getString("Answer") + "n";  
  27.         }  
  28.         setStatus(x);  
  29.     }  
  30.     catch (Exception je)  
  31.     {  
  32.         setStatus("Error w/file: " + je.getMessage());  
  33.     }  
  34. }  
posted on 2011-06-09 16:04  threecc  阅读(77)  评论(0)    收藏  举报