今天刚刚学到json解析,看了一整天,大概了解到json就是你通过一个API(我用的聚合数据的API)发送一个请求,接着会收到json数据,比如说天气预报吧,他会给你发送一大段字符串,大概是未来几天的天气情况了什么的,因为这个数据我们想把它规则的展现在手机屏幕上,但是我们得到的json数据未经处理的话,很乱没有美感,也不方便看,就需要我们解析出这些数据并展现在手机屏幕上

cityName = URLEncoder.encode(city,"UTF-8"); 这句话将汉字转化为UTF-8编码

1.首先是要获取json数据,我是直接使用了已经集成的Volley包,获得的数据,Volley又有get和post方法,这里使用get方法

Volley方法获得json数据:

private void volley_get()
{
String cityName;
String city;
@Override
Toast.makeText(MainActivity.this, cityName, Toast.LENGTH_SHORT).show();
String url = "http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&=你申请的Key";
/*
* StringRequest中的第一个参数,是我们获取数据所调用的方法 Method.GET
*第二个参数,是我们获得的API的接口 url
*第三个参数,是当请求成功时所调用的参数
* 第四个参数,是请求失败时所调用的参数
* */
StringRequest request = new StringRequest(Method.GET, url, new Response.Listener<String>()
{
@Override
public void onResponse(String s)
{//参数s就是我们请求成功时返回的json数据
Toast.makeText(MainActivity.this, "加载数据成功", Toast.LENGTH_SHORT).show();
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError volleyError)
{
Toast.makeText(MainActivity.this, "对不起,加载数据失败", Toast.LENGTH_SHORT).show();
}
});
request.setTag("abcGet");
MyApplication.getHttpQueues().add(request);//因为Volley方法就是通过请求队列的方法实现的,所以这里要加入队列 }); }

获得到的天气数据

{"resultcode":"200","reason":"successed!","result":{"sk":{"temp":"37","wind_direction":"西风","wind_strength":"2级","humidity":"49%","time":"18:28"},"today":{"temperature":"30℃~38℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期二","city":"苏州","date_y":"2016年07月26日","dressing_index":"炎热","dressing_advice":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。","uv_index":"很强","comfort_index":"","wash_index":"较适宜","travel_index":"较适宜","exercise_index":"较适宜","drying_index":""},"future":[{"temperature":"30℃~38℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期二","date":"20160726"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西风微风","week":"星期三","date":"20160727"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期四","date":"20160728"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"南风微风","week":"星期五","date":"20160729"},{"temperature":"28℃~37℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"东风微风","week":"星期六","date":"20160730"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期日","date":"20160731"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西风微风","week":"星期一","date":"20160801"}]},"error_code":0}

然后在上边的volley_get方法中解析json使其规则的显现

在上边的volley_get方法中 请求成功时调用的参数中实现

 public void onResponse(String s) { //参数s就是我们请求成功时返回的json数据
// tv_1.setText(s);
String weather_city = null;
String weather_date_y = null;
String weather_dressing_advice= null;
String weather_exercise_index= null;
String weather_temperature= null;
String weather_travel_index= null;
String weather_wea= null;
String weather_week= null;
String weather_wind= null;
//
try {
JSONObject jsonObject = new JSONObject(s);
String resultcode = jsonObject.getString("resultcode");
if(resultcode.equals("200")){
JSONObject resultObject=jsonObject.getJSONObject("result");
JSONObject todayObject=resultObject.getJSONObject("today");
weather_city = todayObject.getString("city");
weather_date_y = todayObject.getString("date_y");
weather_dressing_advice = todayObject.getString("dressing_advice");
weather_exercise_index = todayObject.getString("exercise_index");
weather_temperature = todayObject.getString("temperature");
weather_travel_index = todayObject.getString("travel_index");
weather_wea = todayObject.getString("weather");
weather_week = todayObject.getString("week");
weather_wind = todayObject.getString("wind");
}
} catch (JSONException e) {
e.printStackTrace();
}
tv_1.setText("城市 :"+weather_city);
tv_2.setText("日期 :"+weather_date_y);
tv_3.setText("穿衣指数 :"+weather_dressing_advice);
tv_4.setText("锻炼指数 :"+weather_exercise_index);
tv_5.setText("温度 :"+weather_temperature);
tv_6.setText("旅行指数 :"+weather_travel_index);
tv_7.setText("天气 :"+weather_wea);
tv_8.setText("星期 :"+weather_week);
tv_9.setText("风力 :"+weather_wind);
}
}

操作结果:

android json解析(JSONObject方法实现)的更多相关文章

  1. Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例

    Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...

  2. Android Json解析与总结

    一.JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Progra ...

  3. android json 解析简单实例

    Android JSON解析跟JAVA 的JSON解析原理是一样的. Android自带的JSON方式跟方便,不需要导包啥的.不深究原理了,直接上代码: public class JsonActivi ...

  4. Android JSON 解析关键代码

    Android Json 解析其实还是蛮重要的知识点,为什么这么说呢,因为安卓通信大部分的协议都是使用 json 的方式传输,我知道以前大部分是使用的 xml ,但是时代在发展社会在进步,json 成 ...

  5. android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】

    android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...

  6. android json解析及简单例子

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  7. android Json解析详解

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语 言的支持),从而可以在不同平台间进行数 ...

  8. android Json解析详解(详细代码)

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  9. 【转】android json解析及简单例子

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

随机推荐

  1. UVA 11291 Smeech

    [来源]https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. NET持续集成与自动化部署

    https://www.cnblogs.com/hunternet/p/9590287.html 相信每一位程序员都经历过深夜加班上线的痛苦!而作为一个加班上线如家常便饭的码农,更是深感其痛.由于我们 ...

  3. LeetCode Longest Continuous Increasing Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...

  4. BZOJ3075,LG3082 [USACO13MAR]项链Necklace

    题意 Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, ...

  5. SVM模型进行分类预测时的参数调整技巧

    一:如何判断调参范围是否合理 正常来说,当我们参数在合理范围时,模型在训练集和测试集的准确率都比较高:当模型在训练集上准确率比较高,而测试集上的准确率比较低时,模型处于过拟合状态:当模型训练集和测试集 ...

  6. nodejs 安装 cnpm 命令

    npm install -g cnpm --registry=https://registry.npm.taobao.org

  7. dockerfile mysql

    FROM centos6.6-mysql5.5:0.0.4 MAINTAINER syberos:wangmo RUN mv /etc/my.cnf /etc/my.cnf.bak ADD my.cn ...

  8. C# 加密和解密文件

    相关示例代码如下: using System; using System.IO; using System.Security; using System.Security.Cryptography; ...

  9. Angular5学习笔记 - 虚拟RestfulApi配置与使用(六)

    一.安装json-server功能 #windows cnpm install json-server -g #Mac & Linux sudo npm install json-server ...

  10. Update多个字段从一个表中

    UPDATE XXXXXX S SET (S.XXX, S.CCC, S.DDD, S.AAA, S.BBB) = (SELECT F.XXX, F.CCC, F.AAA, BBB FROM XXXX ...