android的json解析
来源:互联网 发布:福建厦门广电网络 编辑:程序博客网 时间:2024/05/27 19:27
1.评价:
json解析是最主流的解析第三方接口源数据的方法
2.解析对象的类型:
a.jsonObject
b.jsonArray
3.进行json解析的步骤(以jsonObject为例)
a.获得json的父级标签,例如:JsonObject js = new JsonObject(json数据源)
b.根据父级标签获得子级标签,例如:JsonObject result = js.getJsonObject(“result”)—>注:result是子级标签的名字
c.获得json具体数据:
result.getJsonObject(“result”).getString(“值名称”);
4.代码实例:
asyntask获得天气数据,数据经过json进行解析。
package net.lamp.weatherforcast;import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.media.MediaPlayer;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity { private EditText et; private TextView tv; String cityname; private WeatureInfo weatureinfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.main_et); tv = (TextView) findViewById(R.id.main_tv_weather); } public void getjson(View v) { cityname = et.getText().toString().trim(); if (cityname==null||cityname.equals("")) { cityname="北京"; } String url = "http://v.juhe.cn/weather/index?format=2&cityname="+cityname+"&key=7453f4a7cee3ed86f6fdae5a46e8a78b"; AsyncTask<String, Void, WeatureInfo> task = new AsyncTask<String, Void, WeatureInfo>() { WeatureInfo wi = null; @Override protected WeatureInfo doInBackground(String... params) { // TODO Auto-generated method stub HttpClient hc = new DefaultHttpClient(); HttpGet hg = new HttpGet(params[0]); //发送请求获得响应 try { HttpResponse hr = hc.execute(hg); HttpEntity he = hr.getEntity(); String content = EntityUtils.toString(he, HTTP.UTF_8); //wi获得经过json解析过的javabean对象 wi = getWeatureinfo(content); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return wi; } @Override protected void onPostExecute(WeatureInfo result) { // TODO Auto-generated method stub super.onPostExecute(result); tv.setText(result.toString()); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } @Override protected void onProgressUpdate(Void... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); } }; task.execute(url); } //json解析:参数是String型的json原数据 返回类型:javabean public WeatureInfo getWeatureinfo(String content) { weatureinfo = new WeatureInfo(); try { JSONObject js = new JSONObject(content); JSONObject result =js.getJSONObject("result"); JSONObject sk = result.getJSONObject("sk"); weatureinfo.setTemp(sk.getString("temp")); JSONObject today = result.getJSONObject("today"); weatureinfo.setCity(today.getString("city")); weatureinfo.setTemp_range(today.getString("temperature")); weatureinfo.setTime(today.getString("date_y")); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return weatureinfo; }}
jsonArray的代码实例:
jsonarray ["123",123,true,{"username":"gxx","password":123,"age":28},{"username":"gxx","password":123,"age":28},["123","987"]]
0 0
- Android的JSON解析
- Android的Json解析
- Android的JSON解析
- Android的JSON解析
- android json的解析
- android json的解析
- android的json解析
- Android中json的解析
- android中json的解析
- android的json解析-详解
- android中JSON的解析
- android 的json解析实例
- android中JSON的解析
- Android Json解析的封装
- Android中JSON的解析
- Android的JSON数据解析
- android中JSON的解析
- Json数组的解析--android
- linux 安装jpeg 出错。
- Magento中如何判断一个用户是否已经登陆
- Linux下文件权限更改问题
- FLURRY 文档摘要及备注
- iOS库 .a与.framework区别
- android的json解析
- Html的label的for属性
- 还是畅通工程
- 计算自己活了多少天 SimpleDateFormat Date getTime()
- 利用正则表达式抽取网页信息
- LeetCode-20 Valid Parentheses
- LINQ体验(13)——LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL
- 查税( 斜率优化&单调队列维护凸包 &分块 )
- 使用Eclipse构建Maven项目