两种android解析json的例子
第一个事件是android直接解析json字符串
第二个事件是将json写在文件中,并放在raw目录下 进行解析
工程项目结构在最后附件,双击图片查看原图

完整代码如下:


json形式为:
{   "firstname":"Richard",   "lastname":"Stearns",   "almamater":"Cornell University",   "occupation":"President, World Vision",   "interview":       {           "source" : "http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY",           "questions":               [               {               "Question": "How much money does World Vision raise every year?",               "Answer": "Worldwide, World Vision raises about $2 billion annually; the U.S. office, which I head up, raises about half of the total."               },               {               "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?",               "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.\""               }               ]       }   }   


实现代码:

import java.io.InputStream;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.R.integer;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class JsonAndroidActivity extends Activity implements OnClickListener{private Button jsonbutton,jsonbutton2;private JSONObject obj;private String staticObject = "{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";       public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                jsonbutton = (Button)findViewById(R.id.jsonbutton);        jsonbutton2 = (Button)findViewById(R.id.jsonbutton2);        jsonbutton.setOnClickListener(this);        jsonbutton2.setOnClickListener(this);    }        @Override    public void onClick(View v) {    switch(v.getId()){    case R.id.jsonbutton:{    try {    obj = new JSONObject(staticObject);    String x = obj.get("firstname").toString() + "" + obj.get("lastname").toString() + "has" + obj.getInt("cellphones")+ "Android phones";    setStatus(x);    } catch (JSONException e) {    setStatus("Error occured " + e.getMessage());    }    }        case R.id.jsonbutton2:{    try {String x = null;InputStream is = this.getResources().openRawResource(R.raw.interview);byte [] buffer = new byte[is.available()];while(is.read(buffer)!= -1){String json = new String(buffer);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 e) {setStatus("Error w/file: " + e.getMessage());   }    }    }        }        void setStatus(String x){    TextView tv = (TextView)findViewById(R.id.jsontextView);    tv.setText(x);    }}



layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView android:text="TextView"       android:id="@+id/jsontextView"       android:layout_width="wrap_content"      android:layout_height="wrap_content"/><Button android:text="json" android:id="@+id/jsonbutton" android:layout_width="wrap_content" android:layout_height="wrap_content"/><Button android:text="json2" android:id="@+id/jsonbutton2" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>

更多相关文章

  1. android平台解析epub格式的书籍信息
  2. 在Android中使用Html5
  3. Android(安卓)Duplicate files copied in APK
  4. Android(安卓)webkit 事件传递流程详解
  5. android读取本地网页
  6. android 变长数据GSON解析
  7. Android(安卓)NDK之发送短信
  8. Android(安卓)ListView列表分组
  9. android 子线程handler 实现

随机推荐

  1. 懒骨头的Android文档备份1:建造你的第一个
  2. Android(安卓)Mediacodec硬解H264并显示
  3. Android 微信分享操作后 在当前界面提示
  4. Android PinyinIME 源码笔记 -- 1. 底层
  5. Android事件总线(三)otto用法全解析
  6. Android(安卓)apk快速定位、灰色按钮克星
  7. Android屏幕适配小技巧swdp
  8. Android上使用ksoap2支持Web Service服务
  9. Android ExoPlayer播放音视频的使用指南
  10. Android 常用的命名规范