博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android解析JSON
阅读量:5109 次
发布时间:2019-06-13

本文共 4310 字,大约阅读时间需要 14 分钟。

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

先一个简单的例子,json直接写在string中
Java代码
String staticObject = "{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";  
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());  
    }  
}  
void setStatus(String x)  
{  
    TextView tv = (TextView) findViewById(R.id.txtStatus);  
    tv.setText(x);  
}  
String staticObject = "{\"firstname\":\"Steve\",\"lastname\":\"Jobs\",\"cellphones\":\"0\"}";  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());  } }  void setStatus(String x) {  TextView tv = (TextView) findViewById(R.id.txtStatus);  tv.setText(x); }
将json写在文件中,并放在raw目录下
json形式为
Java代码
{  
"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.\""  
            }  
            ]  
    }  
}  
{ "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.\""    }    ]  } }
其中解析代码为
Java代码
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 = 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());  
    }  

转载于:https://www.cnblogs.com/AlexCheng/archive/2011/05/26/2120028.html

你可能感兴趣的文章
关于TFS2010使用常见问题
查看>>
poj2752 Seek the Name, Seek the Fame
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
ionic2+ 基础
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
Screening technology proved cost effective deal
查看>>
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>