关闭

Android-JSON解析

标签: AndroidJSON解析数据
295人阅读 评论(0) 收藏 举报
分类:

JSON解析记录

下面是将要解析的一个天气json数据

  • 这是一个在谷歌浏览器jsonview插件处理过的显示视图,部分标签相同部分被缩略
{
resultcode: "200",
reason: "successed!",
result: - {
sk: - {
temp: "12",
wind_direction: "西南风",
wind_strength: "1级",
humidity: "66%",
time: "00:20"
},
today: - {
temperature: "12℃~18℃",
weather: "阴转小雨",
weather_id: + {... },
wind: "南风微风",
week: "星期二",
city: "成都",
date_y: "2015年11月17日",
dressing_index: "较舒适",
dressing_advice: "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
uv_index: "最弱",
comfort_index: "",
wash_index: "不宜",
travel_index: "较适宜",
exercise_index: "较适宜",
drying_index: ""
},
future: - [
+ {
    temperature: "12℃~18℃",
    weather: "阴转小雨",
    weather_id: - 
    {
        fa: "02",
        fb: "07"
    },
    wind: "南风微风",
    week: "星期二",
    date: "20151117"
   },
+ {... },
+ {... },
+ {... },
+ {... },
+ {... },
+ {... }
]
},
error_code: 0
}

我们开始解析数据

  1. 首先需要从网上获取JSON数据,转换成字符串
  2. 然后是对数据字符串进行解析,代码如下:
/**
     * json解析
     * @param jsontext
     * @return
     */
    public static String changeJson(String jsontext)
    {
        //用StringBuilder 来装下所有数据,然后在TextView中显示
        StringBuilder sb = new StringBuilder();
        try {
            //通过json数据的字符串得到一个JSONObject对象
            JSONObject obj = new JSONObject(jsontext);
            //我们通过标签名,就可以获取到当前结构下的值
            String resultcode = obj.getString("resultcode");
            sb.append(resultcode+"\n");
            String reason = obj.getString("reason");
            sb.append(reason+"\n");
            //通过外一层JSONObject对象获取下一层JSONObject对象
            JSONObject objResult = obj.getJSONObject("result");
            JSONObject objSk = objResult.getJSONObject("sk");
            String temp = objSk.getString("temp");
            sb.append(temp+"\n");
            String wind_direction = objSk.getString("wind_direction");
            sb.append(wind_direction+"\n");
            String wind_strength = objSk.getString("wind_strength");
            sb.append(wind_strength+"\n");
            String humidity = objSk.getString("humidity");
            sb.append(humidity+"\n");
            String time = objSk.getString("time");
            sb.append(time+"\n");
            JSONObject objToday = objResult.getJSONObject("today");
            String temperature = objToday.getString("temperature");
            sb.append(temperature+"\n");
            String weather = objToday.getString("weather");
            sb.append(weather+"\n");
            JSONObject objWeatherId = objToday.getJSONObject("weather_id");
            String fa = objWeatherId.getString("fa");
            sb.append(fa+"\n");
            String fb = objWeatherId.getString("fb");
            sb.append(fb+"\n");
            String wind = objToday.getString("wind");
            sb.append(wind+"\n");
            String week = objToday.getString("week");
            sb.append(week+"\n");
            String city = objToday.getString("city");
            sb.append(city+"\n");
            String date_y = objToday.getString("date_y");
            sb.append(date_y+"\n");
            String dressing_index = objToday.getString("dressing_index");
            sb.append(dressing_index+"\n");
            String dressing_advice = objToday.getString("dressing_advice");
            sb.append(dressing_advice+"\n");
            String uv_index = objToday.getString("uv_index");
            sb.append(uv_index+"\n");
            String comfort_index = objToday.getString("comfort_index");
            sb.append(comfort_index+"\n");
            String wash_index = objToday.getString("wash_index");
            sb.append(wash_index+"\n");
            String travel_index = objToday.getString("travel_index");
            sb.append(travel_index+"\n");
            String exercise_index = objToday.getString("exercise_index");
            sb.append(exercise_index+"\n");
            String drying_index = objToday.getString("drying_index");
            sb.append(drying_index+"\n");
            //通过外层JSONObject对象获取下层**中括号**内容
            JSONArray arrFuture = objResult.getJSONArray("future");
            //遍历所有中括号下的几个大括号
            for (int i = 0, length = arrFuture.length(); i < length; i++)
            {
                //通过标签获取第i个大括号JSONObject对象
                JSONObject objArr = arrFuture.getJSONObject(i);
                String temperature_a = objArr.getString("temperature");
                sb.append(temperature_a+"\n");
                String weather_a = objArr.getString("weather");
                sb.append(weather_a+"\n");
                JSONObject objWeatherId_a = objArr.getJSONObject("weather_id");
                String fa_a = objWeatherId_a.getString("fa");
                sb.append(fa_a+"\n");
                String fb_a = objWeatherId_a.getString("fb");
                sb.append(fb_a+"\n");
                String wind_a = objArr.getString("wind");
                sb.append(wind_a+"\n");
                String week_a = objArr.getString("week");
                sb.append(week_a+"\n");
                String date_a = objArr.getString("date");
                sb.append(date_a+"\n");
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sb.toString();

    }

我的感受

  • 首先在这个访问地址上获取json数据:(别点,自己申请去~~(>_<)~~)
    http://v.juhe.cn/weather/index?format=2&cityname=%E6%88%90%E9%83%BD&key=ea79dbb1c2858555d8cec5d9309838c8
    问号后面的就是要访问的城市,和key值,选择不同的城市就改变cityname的值
  • 如果我要填入一个城市,上一条的cityname字符串需要转一下码,不然出现了城市错误提示。 ~(~ ̄▽ ̄)~我是这样处理的:
    String city = “成都”;
    city = URLEncoder.encode(city, “utf-8”);
    然后将city拼接到地址上

想说的话

  • 博客坚持写,不管怎样,今后学习了新的东西就在这里记录一下,以便今后回顾,也希望小小笔记能帮助你们
  • 如果内容有什么地方不对,望吐槽,希望大家能一起学习和成长
  • 来一句:人外有人,天外有天。三人行必有我师,问问良师益友,会让你受益无穷。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13255次
    • 积分:403
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    文章分类
    文章存档
    最新评论