Android解析Json例子

转载于http://syab11.iteye.com/blog/736067



package per.lx.json;  
  
import java.io.IOException;  
import java.io.InputStream;  
  
import org.json.JSONArray;  
import org.json.JSONException;  
import org.json.JSONObject;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.TextView;  
  
public class JSONTestActivity extends Activity {  
  
    JSONObject obj;  
  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        buildObjectFromFile();  
    }  
  
    String staticObject = "{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"2\"}";  
  
    public void buildObject() {  
        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 je) {  
            setStatus("Error occured " + je.getMessage());  
        }  
    }  
  
    public void setStatus(String x) {  
        TextView tv = (TextView) findViewById(R.id.txtStatus);  
        tv.setText(x);  
    }  
  
    public 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);  
            obj = new JSONObject(json);  
            x = "name : " + obj.getString("firstname") + " " + obj.getString("lastname") + "\n";  
            x += "occupation : " + 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() + "\n\n";  
  
            for (int i = 0; i < questions.length(); i++) {  
                JSONObject qa = questions.getJSONObject(i);  
                x += "------------\n";  
                x += "Q" + (i + 1) + ". " + qa.getString("Question") + "\n\n";  
                x += "A" + (i + 1) + ". " + qa.getString("Answer") + "\n";  
            }  
            setStatus(x);  
        } catch (JSONException e) {  
            setStatus("Error w/file: " + e.getMessage());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
} 



interview.txt

    {  
    "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.\""  
                }  
                ]  
        }  
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值