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

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

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文档参见:

JSON返回示例:

{    "resultcode": "200",    "reason": "SUCCESSED!",    "result": [        {            "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

运行效果:
SouthEast

代码清单:

布局文件:activity_main.xml

"

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
{ 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
headerList = new ArrayList
(); headerList.add(new BasicNameValuePair("Content-Type", "text/html; charset=utf-8")); String targetUrl = JUHE_URL_ENVIRONMENT_AIR_PM; ArrayList
paramList = new ArrayList
(); 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

JSONDemo
Settings
Hello world!
城市
PM2.5指数
空气质量指数
空气质量
PM10
一氧化碳
二氧化氮
臭氧
二氧化硫
更新时间

API知识点

public class
JSONObject
extends Object

org.json.JSONObject

Class Overview

A modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL. Values may not be null, NaNs, infinities, or of any type not listed here.

JSONObject(String json)

Creates a new JSONObject with name/value mappings from the JSON string.

Object get(String name)

Returns the value mapped by name.

int getInt(String name)

Returns the value mapped by name if it exists and is an int or can be coerced to an int.

String getString(String name)

Returns the value mapped by name if it exists, coercing it if necessary.

JSONArray getJSONArray(String name)

Returns the value mapped by name if it exists and is a JSONArray.

public class

JSONArray
extends Object

org.json.JSONArray

Class Overview

A dense indexed sequence of values. Values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or NULL. Values may not be NaNs, infinities, or of any type not listed here.

JSONObject getJSONObject(int index)

Returns the value at index if it exists and is a JSONObject.

转载地址:http://irfcl.baihongyu.com/

你可能感兴趣的文章
网页适配之强制网页适配手机屏幕
查看>>
ThinkPHP2.2 任意代码执行漏洞 利用
查看>>
期年之痒
查看>>
cpanm perl模块自动安装
查看>>
深入讲解BGP协议synchronous原理
查看>>
简要总结最近遇到的5个问题
查看>>
中国象棋程序的设计与实现(七)--心得体会和开发日志
查看>>
浅显理解 Python 闭包
查看>>
学习Oracle分析函数(Analytic Functions)
查看>>
openstack学习笔记二 网络设置基础
查看>>
RabbitMQ基础
查看>>
有了安全边界,人工智能才能有序发展
查看>>
Qt在mainwindow下代码添加控件不能显示的问题
查看>>
【cocos2dx】使用VS插件在VS2012/2013上编辑和调试Quick-Cocos2d-x的Lua代码
查看>>
Centos6.0之pptpd+mysql+freeradius实现***帐号统一认证管理
查看>>
Excel导出数据
查看>>
解释Windows7“上帝模式”的原理
查看>>
httpClient4.* 使用教程
查看>>
相对和绝对路径、cd命令、创建和删除目录mkdir/rmdir 、rm命令
查看>>
yum安装配置nagios
查看>>