Android之JSON格式数据解析
查看原文:http://blog.csdn.net/hantangsongming/article/details/42234293
JSON:JavaScript 对象表示法(JavaScript Object Notation)。独立于语言和平台,比 XML 更小、更快,更易解析。如今JSON数据已经成为了互联网中大多数数据的传递方式,所以必须要熟练掌握。
Android平台自带了JSON解析的相关API,可以将文件、输入流中的数据转化为JSON对象,然后从对象中获取JSON保存的数据内容。
Android的JSON解析部分都在包org.json下,主要有以下几个类:
JSONObject:可以看作是一个json对象,这是系统中有关JSON定义的基本单元,其包含一对儿
(Key/Value)数值。它对外部(External:应用toString()方法输出的数值)调用的响应体现为一个标准的字符串(例如:
{"JSON": "Hello,
World"},最外被大括号包裹,其中的Key和Value被冒号":"分隔)。其对于内部(Internal)行为的操作格式略微,例如:初始化一个
JSONObject实例,引用内部的put()方法添加数值:new
JSONObject().put("JSON", "Hello,
World!"),在Key和Value之间是以逗号","分隔。Value的类型包括:Boolean、JSONArray、JSONObject、
Number、String或者默认值JSONObject.NULL object。
JSONStringer:json文本构建类 ,根据官方的解释,这个类可以帮助快速和便捷的创建JSON
text。其最大的优点在于可以减少由于 格式的错误导致程序异常,引用这个类可以自动严格按照JSON语法规则(syntax
rules)创建JSON text。每个JSONStringer实体只能对应创建一个JSON
text。。其最大的优点在于可以减少由于格式的错误导致程序异常,引用这个类可以自动严格按照JSON语法规则(syntax
rules)创建JSON text。每个JSONStringer实体只能对应创建一个JSON
text。
JSONArray:它代表一组有序的数值。将其转换为String输出(toString)所表现的形式是用方括号
包裹,数值以逗号”,”分隔(例如:[value1,value2,value3],大家可以亲自利用简短的代码更加直观的了解其格式)。这个类的内部同
样具有查询行为,get()和opt()两种方法都可以通过index索引返回指定的数值,put()方法用来添加或者替换数值。同样这个类的value
类型可以包括:Boolean、JSONArray、JSONObject、Number、String或者默认值JSONObject.NULL
object。
JSONTokener:json解析类
JSONException:json中用到的异常
下面以聚合数据空气质量城市空气PM2.5指数数据接口为例来演示JSON格式数据的解析。
聚合数据空气质量城市空气PM2.5指数数据接口API文档参见:http://www.juhe.cn/docs/api/id/33/aid/79
JSON返回示例:
{ /*JSONObject*/
"resultcode": "200",
"reason": "SUCCESSED!",
"result": [ /*JSONArray*/
{ /*JSONObject*/
"city": "苏州", /*城市*/
"PM2.5": "73", /*PM2.5指数*/
"AQI": "98", /*空气质量指数*/
"quality": "良", /*空气质量*/
"PM10": "50",/*PM10*/
"CO": "0.79", /*一氧化碳*/
"NO2": "65", /*二氧化氮*/
"O3": "28", /*臭氧*/
"SO2": "41", /*二氧化硫*/
"time": "2014-12-26 11:48:40"/*更新时间*/
}
],
"error_code": 0
}
实例:JSONDemo
运行效果:
代码清单:
布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:gravity="center"
android:text="城市:"
android:textSize="23sp" /> <EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:inputType="text" />"
</LinearLayout> <Button
android:id="@+id/query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
android:textSize="23sp" /> <TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Java源代码文件:MainActivity.java
package com.rainsong.jsondemo; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
EditText et_city;
Button btn_query;
TextView tv_result;
QueryTask task; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_city = (EditText)findViewById(R.id.city);
tv_result = (TextView)findViewById(R.id.result);
btn_query = (Button)findViewById(R.id.query); btn_query.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String city = et_city.getText().toString();
if (city.length() < 1) {
Toast.makeText(MainActivity.this, "请输入城市名",
Toast.LENGTH_LONG).show();
return;
}
task = new QueryTask(MainActivity.this, tv_result);
task.execute(city);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
Java源代码文件:QueryTask.java
package com.rainsong.jsondemo; import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast; public class QueryTask extends AsyncTask<String, Void, String> {
Context context;
TextView tv_result; private static final String JUHE_URL_ENVIRONMENT_AIR_PM =
"http://web.juhe.cn:8080/environment/air/pm";
private static final String JUHE_APPKEY = "你申请的APPKEY值"; public QueryTask(Context context, TextView tv_result) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.tv_result = tv_result;
} @Override
protected String doInBackground(String... params) {
String city = params[0]; ArrayList<NameValuePair> headerList = new ArrayList<NameValuePair>();
headerList.add(new BasicNameValuePair("Content-Type", "text/html; charset=utf-8")); String targetUrl = JUHE_URL_ENVIRONMENT_AIR_PM; ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
paramList.add(new BasicNameValuePair("key", JUHE_APPKEY));
paramList.add(new BasicNameValuePair("dtype", "json"));
paramList.add(new BasicNameValuePair("city", city)); for (int i = 0; i < paramList.size(); i++) {
NameValuePair nowPair = paramList.get(i);
String value = nowPair.getValue();
try {
value = URLEncoder.encode(value, "UTF-8");
} catch (Exception e) {
}
if (i == 0) {
targetUrl += ("?" + nowPair.getName() + "=" + value);
} else {
targetUrl += ("&" + nowPair.getName() + "=" + value);
}
} HttpGet httpRequest = new HttpGet(targetUrl);
try {
for (int i = 0; i < headerList.size(); i++) {
httpRequest.addHeader(headerList.get(i).getName(),
headerList.get(i).getValue());
} HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = EntityUtils.toString(httpResponse.getEntity());
return strResult;
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
} @Override
protected void onPostExecute(String result) {
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
int resultCode = jsonObject.getInt("resultcode");
if (resultCode == 200) {
JSONArray resultJsonArray = jsonObject.getJSONArray("result");
JSONObject resultJsonObject = resultJsonArray.getJSONObject(0);
String output = context.getString(R.string.city) + ": " + resultJsonObject.getString("city") + "\n"
+ context.getString(R.string.PM25) + ": " + resultJsonObject.getString("PM2.5") + "\n"
+ context.getString(R.string.AQI) + ": " + resultJsonObject.getString("AQI") + "\n"
+ context.getString(R.string.quality) + ": " + resultJsonObject.getString("quality") + "\n"
+ context.getString(R.string.PM10) + ": " + resultJsonObject.getString("PM10") + "\n"
+ context.getString(R.string.CO) + ": " + resultJsonObject.getString("CO") + "\n"
+ context.getString(R.string.NO2) + ": " + resultJsonObject.getString("NO2") + "\n"
+ context.getString(R.string.O3) + ": " + resultJsonObject.getString("O3") + "\n"
+ context.getString(R.string.SO2) + ": " + resultJsonObject.getString("SO2") + "\n"
+ context.getString(R.string.time) + ": " + resultJsonObject.getString("time") + "\n";
tv_result.setText(output);
} else if (resultCode == 202) {
String reason = jsonObject.getString("reason");
tv_result.setText(reason);
} else {
Toast.makeText(context, "查询失败",
Toast.LENGTH_LONG).show();
tv_result.setText("");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(context, "查询失败",
Toast.LENGTH_LONG).show();
tv_result.setText("");
}
} }
字符串资源:string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">JSONDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string name="city">城市</string>
<string name="PM25">PM2.5指数</string>
<string name="AQI">空气质量指数</string>
<string name="quality">空气质量</string>
<string name="PM10">PM10</string>
<string name="CO">一氧化碳</string>
<string name="NO2">二氧化氮</string>
<string name="O3">臭氧</string>
<string name="SO2">二氧化硫</string>
<string name="time">更新时间</string> </resources>
Android之JSON格式数据解析的更多相关文章
- Android读取JSON格式数据
Android读取JSON格式数据 1. 何为JSON? JSON,全称为JavaScript Object Notation,意为JavaScript对象表示法. JSON 是轻量级的文本数据交换格 ...
- Android创建JSON格式数据
Android创建JSON格式数据 作为上一篇博客的补充,简单那解说了一下Android创建JSON格式数据的小Demo. 1. 创建JSON格式数据 对于Android创建JSON格式数据.因为An ...
- html中通过js获取接口JSON格式数据解析以及跨域问题
前言:本人自学前端开发,一直想研究下js获取接口数据在html的实现,顺利地找到了获取数据的方法,但是有部分接口在调用中出现无法展示数据.经查,发现时跨域的问题,花费了一通时间,随笔记录下过程,以方便 ...
- 实现android上解析Json格式数据功能
实现android上解析Json格式数据功能,该源码转载于安卓教程网的,http://android.662p.com ,个人感觉还不错的,大家可以看看一下吧. package com.practic ...
- app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上
上一篇文章写的是服务器端利用Servlet 返回JSON字符串,本文主要是利用android客户端访问服务器端链接,解析JSON格式数据,放到相应的位置上. 首先,android程序的布局文件main ...
- 解析json格式数据
实现目标 读取文件中的json格式数据,一行为一条json格式数据.进行解析封装成实体类. 通过google的Gson对象解析json格式数据 我现在解析的json格式数据为: {",&qu ...
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
目录 前言 1 Get方式和Post方式接口说明 2 OkHttp库简单介绍及环境配置 3 具体实现 前言 本文具体实现思路和大部分代码参考自<第一行代码>第2版,作者:郭霖:但是文中讲 ...
- iOS开发之JSON格式数据的生成与解析
本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...
- Android读写JSON格式的数据之JsonWriter和JsonReader
近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...
随机推荐
- Django 项目连接数据库Mysql要安装mysqlclient驱动出错 : Failed building wheel for mysqlclient:
1,如果直接用 CMD命令:pip install mysqlclient ,会安装出错. 2,解决问题,参考了这个博友的帖子:https://blog.csdn.net/qq_29784441/ar ...
- 八皇后问题的Python实现和C#实现
看到八皇后问题的解决思路, 感觉很喜欢. 我用C#实现的版本之前贴在了百度百科上(https://baike.baidu.com/item/%E5%85%AB%E7%9A%87%E5%90%8E%E9 ...
- MVC设计模式在网站中的应用
MVC设计模式在网站中的应用 以淘宝的购物车为例 一.结合六个基本质量属性 可修改性 采用MVC设计模式的时候,可以将视图.模型.控制器分析,将用户动作.数据表示.应用数据分离开来,如果用户需要以不同 ...
- cf C. Finite or not? 数论
You are given several queries. Each query consists of three integers pp, qq and bb. You need to answ ...
- Ceph的BlueStore总体介绍
整体架构 bluestore的诞生是为了解决filestore自身维护一套journal并同时还需要基于系统文件系统的写放大问题,并且filestore本身没有对SSD进行优化,因此bluestore ...
- go标准库的学习-sync互斥
https://studygolang.com/pkgdoc 导入方法: import "sync" sync包提供了基本的同步基元,如互斥锁.除了Once和WaitGroup类型 ...
- ORA-245: In RAC environment from 11.2 onwards Backup Or Snapshot controlfile needs to be in shared location (Doc ID 1472171.1)
巡检时遇到错误如下: alert日志: Wed Dec 19 01:00:29 2018Errors in file /oracle/base/diag/rdbms/usap/usap1/trace/ ...
- 在centos7上修改docker加速镜像为阿里云
使用docker pull,命令下载镜像太慢了,默认是从国外的,本文记录下如何配置国内阿里云竞相加速方式. 登录https://cr.console.aliyun.com,如下, 阿里云会为每个用户提 ...
- 从Excel表导入数据到Table
步骤: 1.写第一行SQL,(本sql对应的是oracle数据库) ="INSERT INTO TD_PROMOTION_RATE VALUES("&A3&&quo ...
- 【转】Kaggle注册问题-验证码和手机短信
注册和登录Kaggle时验证码无法显示问题 参考:https://blog.csdn.net/zhuisaozhang1292/article/details/81529981 应用FQ软件需要时时关 ...