让我们一起爱米兰
站内搜搜:
移动设备
请扫描二维码
或访问
m.milan100.com
您所在的位置 -> 米兰百分百 -> android相关 -> android解析Json

android解析Json

点击数:1603 发表时间:2014-03-20 06:14:38 作者: 来源链接:
分享到:
分享到微信
android框架已经为我们集成了解析json的包


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


Java代码

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

  

void buildObject()  

{  

  try  

 {  

  JSONObject obj = new JSONObject(staticObject);  

 String x = obj.get("firstname").toString() + " " + obj.get("lastname").toString() + " has " + obj.getInt("cellphones") + " Android phones.";  

 setStatus(x);  
 }  
 catch (JSONException je)  

 {  
    setStatus("Error occured " + je.getMessage());  

  }  

}   

void setStatus(String x)  

{  

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

    tv.setText(x);  

}  




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

Json代码
  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代码为


 

public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		buildObjectFromFile();
	}

	void buildObjectFromFile() {
		try {
			String x = "";
			InputStream is = this.getResources().openRawResource(
					R.raw.interview);
			byte[] buffer = new byte[is.available()];
			while (is.read(buffer) != -1)
				;
			String json = new String(buffer);
			JSONObject obj = new JSONObject(json);
			x = obj.getString("firstname") + " " + obj.getString("lastname")
					+ "n";
			x += obj.getString("occupation") + "n";
			JSONObject interview = obj.getJSONObject("interview");
			x += "Interview source:" + interview.getString("source") + "n";
			JSONArray questions = interview.getJSONArray("questions");
			x += "Number of questions: " + questions.length() + "nn";
			int i;
			for (i = 0; i < questions.length(); i++) {
				JSONObject qa = questions.getJSONObject(i);
				x += "------------n";
				x += "Q" + (i + 1) + ". " + qa.getString("Question") + "nn";
				x += "A" + (i + 1) + ". " + qa.getString("Answer") + "n";
			}
			setStatus(x);
		} catch (Exception je) {
			setStatus("Error w/file: " + je.getMessage());
		}
	}

	private void setStatus(String x) {
		System.out.println(x);
	}

0
很 好
0
一 般
0
差 劲
热门新闻
相关文章
上一篇: Android通过http协议POST传输方式
下一篇: adb工具的使用
评论区
匿名

返回首页 | 收藏本页 | 回到顶部
Copyright 2010. 米兰百分百 Powered By Bridge.
京ICP备15050557号