android JSON详解

引言:
21世纪是一个信息时代,更是一个网络全球化的时代,随着科学技术的发展近几年android手机越来越普及到每一个人,每一个家庭,随着移动互联网和智能家居的兴起,android手机的变得更加强大,更加方便,也更加完善。越来越多的App不光是需要和网络服务器进行数据传输和交互,也需要和其他 App 进行数据传递。承担App与网络来进行传输和存储数据的一般是XML或者JSON。在软件通信领域中XML和JSON扮演重要角。
最近一段时间,小编综合了之前在项目中对JSON的一些了解,参考了一些相关资料,把自己的一些思考融入了这篇总结文档中,同事尝试用通俗诙谐的语言风格来阐述,期望能给感兴趣的读者带来帮助。

为了不和时代落伍,我们必须积极响应和号召党的领导,所以必须要学习 JSON数据的解析和存储,其实很Easy的。
一、JSON
1.1、JSON是什么?
JSON:如果你搞过网站肯定知道JavaScript这个脚本语言,没搞过也没关系,小编也没搞过网站,JavaScript对象表示法(JavaScript Object Notation), 是一种轻量级的数据交换格式, 易于人阅读和编写, 同时也易于机器解析和生成。

JSON是存储和交换文本信息的语法,类似XML。
JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语
1.2 、JSON格式
JSON构建于两种结构:
1.“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组(associative array)。
2.值的有序列表(An ordered list of values)。在大多数语言中,它被理解为数组(array)、矢量(vector), 列表(list)或者是序列(sequence)。
JSON具有以下这些形式:
1.简单的一个 { ,,,,}。
2.里面有数组 { , [{,,},{,,}],}。
3.直接一个数组。
4.数组里有数组。
5.其他情况? 没有吧,除非json数据不规范。
1.3、解析json数据常用方法
包括JSONObject、JSONArray、JSONStringer、JSONTokener、JSONWriter、JSONEx。
二、JSON解析
前面讲了那么多废话其实也是必须的“毕竟十万个为什么吗“,咱们今天主要讲JSON数据格式的解析,在这呢小编先给大家看一个咱们经常用到的JSON格式的数据,这是聚合官网中“全国天气预报”请求返回的数据格式,格式稍微有点复杂不过也是我们在项目中经常用到的JSON格式,大家都清楚JSON格式的数据是由对象和数组两者结合起来的,再怎么复杂也只有这两种的结合。
JSON数据样式:

     {
 "resultcode":"200",
 "reason":"successed!",
 "result":{
  "sk":{
   "temp":"30",
   "wind_direction":"南风",
   "wind_strength":"3级",
   "humidity":"25%",
   "time":"14:26"
  },
  "today":{
   "temperature":"13℃~31℃",
   "weather":"小雨",
   "weather_id":{
    "fa":"07",
    "fb":"07"
   },
   "wind":"微风",
   "week":"星期日",
   "city":"太原",
   "date_y":"2016年05月01日",
   "dressing_index":"热",
   "dressing_advice":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",
   "uv_index":"弱",
   "comfort_index":"",
   "wash_index":"不宜",
   "travel_index":"较不宜",
   "exercise_index":"较不宜",
   "drying_index":""
  },
  "future":{
   "day_20160501":{
    "temperature":"13℃~31℃",
    "weather":"小雨",
    "weather_id":{
     "fa":"07",
     "fb":"07" },
    "wind":"微风",
    "week":"星期日",
    "date":"20160501"
   },
   "day_20160502":{
    "temperature":"8℃~22℃",
    "weather":"多云转晴",
    "weather_id":{
     "fa":"01",
     "fb":"00" },
    "wind":"西北风4-5 级",
    "week":"星期一",
    "date":"20160502"
   },
   "day_20160503":{
    "temperature":"9℃~24℃",
    "weather":"晴",
    "weather_id":{
     "fa":"00",
     "fb":"00" },
    "wind":"西北风3-4 级",
    "week":"星期二",
    "date":"20160503"
   },
   "day_20160504":{
    "temperature":"14℃~30℃",
    "weather":"晴",
    "weather_id":{
     "fa":"00",
     "fb":"00" },
    "wind":"北风微风",
    "week":"星期三",
    "date":"20160504"
   },
   "day_20160505":{
    "temperature":"9℃~25℃",
    "weather":"多云转晴",
    "weather_id":{
     "fa":"01",
     "fb":"00" },
    "wind":"微风",
    "week":"星期四",
    "date":"20160505"
   },
   "day_20160506":{
    "temperature":"9℃~24℃",
    "weather":"晴",
    "weather_id":{
     "fa":"00",
     "fb":"00" },
    "wind":"西北风3-4 级",
    "week":"星期五",
    "date":"20160506"
   },
   "day_20160507":{
    "temperature":"9℃~24℃",
    "weather":"晴",
    "weather_id":{
     "fa":"00",
     "fb":"00" },
    "wind":"西北风3-4 级",
    "week":"星期六",
    "date":"20160507"
   }
  }
 },
 "error_code":0
}

详细解析:

/**
* 解析当前天气状况信息
* @param responseString
*/
public void split_SK(String responseString){

    // 保存服务器请求回来的数据
    m_Result_Sk_Response_String = responseString;
    // 如果请求回来的数据不为null,进行相应的操作转换
    if(m_Result_Sk_Response_String!=null){

        // 将请求回来的字符串进行转换,转换成数组格式的
        m_JsonObject = JSONObject.fromObject(m_Result_Sk_Response_String);
        // 获取返回状态码
        m_Result_Sk_Response_String = m_JsonObject.getString("resultcode");
        // 如果状态码是200说明返回数据成功
        if((m_Result_Sk_Response_String != null) && m_Result_Sk_Response_String.equals("200")){

            // 得到键"result"中的 所有数据
            m_Result_Sk_Response_String = m_JsonObject.getString("result");
            m_JsonObject = JSONObject.fromObject(m_Result_Sk_Response_String);
            // 此时result中数据有多个key,可以对其key进行遍历,得到某个属性
            m_Result_Sk_Response_String = m_JsonObject.getString("sk");
            m_JSONObject_Sk = JSONObject.fromObject(m_Result_Sk_Response_String);
            Log.i("33333333", ",,,,,,,,,,,,,,,,");

            Message msg = new Message();
            msg.obj = m_JSONObject_Sk;
            msg.what = 0x01;
            m_Weather_Response_String_To_DateBase.getM_My_Weather_Response_String_To_DateBase().sendMessage(msg);
        }else{

            Toast.makeText(m_Context, "请求数据失败!!!", Toast.LENGTH_SHORT).show();
        }
    }else{

        Toast.makeText(m_Context, "服务器无响应!!!", T    oast.LENGTH_SHORT).show();
    }

}

/**
* 解析今天天气状况信息
* @param responseString
*/
public void split_Today(String responseString){

    // 保存服务器请求回来的数据
    m_Result_Today_Response_String = responseString;
    // 如果请求回来的数据不为null,进行相应的操作转换
    if(m_Result_Today_Response_String!=null){
        // 将请求回来的字符串进行转换,转换成数组格式的
        m_JsonObject = JSONObject.fromObject(m_Result_Today_Response_String);
        // 获取返回状态码
        m_Result_Today_Response_String = m_JsonObject.getString("resultcode");
        // 如果状态码是200说明返回数据成功
        if((m_Result_Today_Response_String != null) && m_Result_Today_Response_String.equals("200")){
            // 得到键"result"中的 所有数据
            m_Result_Today_Response_String = m_JsonObject.getString("result");
            m_JsonObject=JSONObject.fromObject(m_Result_Today_Response_String);
            // 今日温度对应的key是today
            m_Result_Today_Response_String = m_JsonObject.getString("today");
            Log.i("fafadafasf", m_Result_Today_Response_String);
            m_JSONObject_Today = JSONObject.fromObject(m_Result_Today_Response_String);

            Log.i("2323232323", m_Result_Today_Response_String);
            Message msg = new Message();
            msg.obj = m_JSONObject_Today;
            msg.what = 0x02;
            m_Weather_Response_String_To_DateBase.getM_My_Weather_Response_String_To_DateBase().sendMessage(msg);
        }else{

            Toast.makeText(m_Context, "请求数据失败!!!", Toast.LENGTH_SHORT).show();
        }
    }else{

        Toast.makeText(m_Context, "服务器无响应!!!", Toast.LENGTH_SHORT).show();
    }

}

/**
* 解析未来七天天气状况信息
* @param responseString
*/
public void split_Future(String responseString){

    // 保存服务器请求回来的数据
    m_Result_Future_Response_String = responseString;
    // 如果请求回来的数据不为null,进行相应的操作转换
    if(m_Result_Future_Response_String!=null){
        // 将请求回来的字符串进行转换,转换成数组格式的
        m_JsonObject = JSONObject.fromObject(m_Result_Future_Response_String);
        // 获取返回状态码
        m_Result_Future_Response_String = m_JsonObject.getString("resultcode");
        // 如果状态码是200说明返回数据成功
        if((m_Result_Future_Response_String != null) && m_Result_Future_Response_String.equals("200")){
            // 得到键"result"中的 所有数据
            m_Result_Future_Response_String = m_JsonObject.getString("result");
            m_JsonObject=JSONObject.fromObject(m_Result_Future_Response_String);
            // 今日温度对应的key是today
            m_Result_Future_Response_String = m_JsonObject.getString("future");
            m_JSONArray_Future = JSONArray.fromObject(m_Result_Future_Response_String);

            Message msg = new Message();
            msg.obj = m_JSONArray_Future;
            msg.what = 0x03;
            m_Weather_Response_String_To_DateBase.getM_My_Weather_Response_String_To_DateBase().sendMessage(msg);
        }else{

            Toast.makeText(m_Context, "请求数据失败!!!", Toast.LENGTH_SHORT).show();
        }
    }else{

        Toast.makeText(m_Context, "服务器无响应!!!", Toast.LENGTH_SHORT).show();
    }
}数组或对象内容进行详细解析:
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        switch(msg.what){
        case 0x01:{

            m_JSONObject_Sk_Info = (JSONObject) msg.obj;
            m_Weather_Current_States_Class = new Weather_Current_States_Class();
            m_Weather_Current_States_Class.setM_City_Name(m_City_Name);
            Log.i("fafadfddddd", m_Weather_Current_States_Class.getM_City_Name());
            m_Weather_Current_States_Class.setM_Temp(m_JSONObject_Sk_Info.getString("temp"));
            m_Weather_Current_States_Class.setM_Wind_Direction(m_JSONObject_Sk_Info.getString("wind_direction"));
            m_Weather_Current_States_Class.setM_Wind_Strength(m_JSONObject_Sk_Info.getString("wind_strength"));
            m_Weather_Current_States_Class.setM_Humidity(m_JSONObject_Sk_Info.getString("humidity"));
            m_Weather_Current_States_Class.setM_Time(m_JSONObject_Sk_Info.getString("time"));
            m_Weather_Current_States_Class_List.add(m_Weather_Current_States_Class);

            m_Mgr.create_Weather_Sk_Info_Table();
            m_Weather_Current_States_Class_ListBack = m_Mgr.query_Weather_Sk_Info("weather_sk_info");
            if(m_Weather_Current_States_Class_ListBack.size() == 0){

                Log.i("m_Weather_Sk_Query_Info.size()", "update_Weather_Sk_Info");
                m_Mgr.insert_Weather_Sk_Info(m_Weather_Current_States_Class_List);
            }else{

                for(int i = 0; i < m_Weather_Current_States_Class_ListBack.size(); i++){

                    if(m_Weather_Current_States_Class_ListBack.get(i).getM_City_Name().equals(m_City_Name)){

                        m_Mgr.update_Weather_Sk_Info(m_Weather_Current_States_Class_List, "weather_sk_info");
                        isQural = true;
                        break;
                    }else{

                        isQural = false;
                    }
                }
                if(isQural == false){

                    Log.i("fafasdafadf", "????????????????????????");
                    m_Mgr.insert_Weather_Sk_Info(m_Weather_Current_States_Class_List);
                }
            }

            break;
        }
        case 0x02:{

            m_JSONObject_Today_Info = (JSONObject) msg.obj;
            m_Weather_Today_States_Class = new Weather_Today_States_Class();
            m_Weather_Today_States_Class.setM_Temperature(m_JSONObject_Today_Info.getString("temperature"));
            m_Weather_Today_States_Class.setM_Weather_States(m_JSONObject_Today_Info.getString("weather"));
            m_JSONObject_Weather_Id_Info = JSONObject.fromObject(m_JSONObject_Today_Info.getString("weather_id"));
            m_Weather_Today_States_Class.getM_Weather_Id().setM_Weather_Fa(m_JSONObject_Weather_Id_Info.getString("fa"));
            m_Weather_Today_States_Class.getM_Weather_Id().setM_Weather_Fb(m_JSONObject_Weather_Id_Info.getString("fb"));
            m_Weather_Today_States_Class.setM_Wind(m_JSONObject_Today_Info.getString("wind"));
            m_Weather_Today_States_Class.setM_Week(m_JSONObject_Today_Info.getString("week"));
            m_Weather_Today_States_Class.setM_City(m_City_Name);
            m_Weather_Today_States_Class.setM_Date_Y(m_JSONObject_Today_Info.getString("date_y"));
            m_Weather_Today_States_Class.setM_Dressing_Index(m_JSONObject_Today_Info.getString("dressing_index"));
            m_Weather_Today_States_Class.setM_Dressing_Advice(m_JSONObject_Today_Info.getString("dressing_advice"));
            m_Weather_Today_States_Class.setM_Uv_Index(m_JSONObject_Today_Info.getString("uv_index"));
            m_Weather_Today_States_Class.setM_Comfort_Index(m_JSONObject_Today_Info.getString("comfort_index"));
            m_Weather_Today_States_Class.setM_Wash_Index(m_JSONObject_Today_Info.getString("wash_index"));
            m_Weather_Today_States_Class.setM_Travel_Index(m_JSONObject_Today_Info.getString("travel_index"));
            m_Weather_Today_States_Class.setM_Exercise_Index(m_JSONObject_Today_Info.getString("exercise_index"));
            m_Weather_Today_States_Class.setM_Drying_Index(m_JSONObject_Today_Info.getString("drying_index"));
            m_Weather_Today_States_Class_List.add(m_Weather_Today_States_Class);

            m_Mgr.create_Weather_Today_Info_Table();
            m_Weather_Today_States_class_ListBack = m_Mgr.query_Weather_Today_Info("weather_today_info");


            if(m_Weather_Today_States_class_ListBack.size() == 0){

                m_Mgr.insert_Weather_Today_Info(m_Weather_Today_States_Class_List);
            }else{

                for(int i = 0; i < m_Weather_Today_States_class_ListBack.size(); i++){

                    if(m_Weather_Today_States_class_ListBack.get(i).getM_City().equals(m_City_Name)){

                        m_Mgr.update_Weather_Today_Info(m_Weather_Today_States_Class_List, "weather_today_info");
                        isQural = true;
                        break;
                    }else{

                        isQural = false;
                    }
                }
                if(isQural == false){

                    Log.i("fafasdafadf", "????????????????????????");
                    m_Mgr.insert_Weather_Today_Info(m_Weather_Today_States_Class_List);
                }
            }

            break;
        }
        case 0x03:{

            m_JSONArray_Future_Info = (JSONArray) msg.obj;
            int m_Future_Weather_City_ID = 0;
            for(Object object:m_JSONArray_Future_Info){//遍历数组

                m_JSONObject_Future_Info = JSONObject.fromObject(object.toString());
                m_Weather_Future_States_Class = new Weather_Future_States_Class();
                m_Weather_Future_States_Class.setM_City_Id(m_Future_Weather_City_ID);
                m_Weather_Future_States_Class.setM_City_Name(m_City_Name);
                Log.i(">>>>>>><<<<<<<<<<<>>>>>>>???????", m_Weather_Future_States_Class.getM_City_Name());
                m_Weather_Future_States_Class.setM_Temperature(m_JSONObject_Future_Info.getString("temperature"));
                m_Weather_Future_States_Class.setM_Weather(m_JSONObject_Future_Info.getString("weather"));
                m_JSONObject_Weather_Id_Info = JSONObject.fromObject(m_JSONObject_Future_Info.getString("weather_id"));
                m_Weather_Future_States_Class.getM_Weather_Id().setM_Weather_Fa(m_JSONObject_Weather_Id_Info.getString("fa"));
                m_Weather_Future_States_Class.getM_Weather_Id().setM_Weather_Fb(m_JSONObject_Weather_Id_Info.getString("fb"));
                m_Weather_Future_States_Class.setM_Wind(m_JSONObject_Future_Info.getString("wind"));
                m_Weather_Future_States_Class.setM_Week(m_JSONObject_Future_Info.getString("week"));
                m_Weather_Future_States_Class.setM_Date(m_JSONObject_Future_Info.getString("date"));
                Log.i(">>>>>>><<<<<<<<<<<>>>>>>>???????", m_Weather_Future_States_Class.getM_Date());
                Log.i(">>>>>>><<<<<<<<<<<", m_Future_Weather_City_ID+"");
                m_Future_Weather_City_ID++;
                m_Weather_Future_States_Class_List.add(m_Weather_Future_States_Class);
            }

            m_Mgr.create_Weather_Future_Info_Table();
            m_Weather_Future_States_Class_ListBack = m_Mgr.query_Weather_Future_Info("weather_future_info", m_City_Name);

            if(m_Weather_Future_States_Class_ListBack.size() == 0){

                m_Mgr.insert_Weather_Future_Info(m_Weather_Future_States_Class_List);
            }else{

                for(m_Future_Weather_City_ID = 0; m_Future_Weather_City_ID < m_Weather_Future_States_Class_ListBack.size(); m_Future_Weather_City_ID=m_Future_Weather_City_ID+7){
                    Log.i("fafasdafadf", "????????????????????????");

                    if(m_Weather_Future_States_Class_ListBack.get(m_Future_Weather_City_ID).getM_City_Name().equals(m_City_Name)){
                        Log.i"ffasdafadf", "????????????????????????");
                        m_Mgr.update_Weather_Future_Info(m_Weather_Future_States_Class_List, "weather_future_info");

                        break;
                    }else{

                        isQural = false;
                    }
                }
                if(isQural == false){

                    m_Mgr.insert_Weather_Future_Info(m_Weather_Future_States_Class_List);
                }
            }

            break;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值