[入门阅读]怎样在android中解析JSON
JSON入门介绍:http://kirin.javaeye.com/blog/616226
也参考了此篇:http://blog.163.com/fushaolin@126/blog/static/16341724220108244251686/
json数据格式解析我自己分为两种:一种是普通的,一种是带有数组形式的:http://archive.cnblogs.com/a/1925327/
先实例化个JSONObject对象
[java] view plaincopyprint?
JSONObject aJosnObj = new JSONObject(jsonStr);//jsonStr为对应json字符串数据
然后再根据json数据的实际情况调用有关方法。
这是一个利用JSON定位的例子:
/**
* Google定位的实现.<br/>
* Geolocation的详细信息请参见:<br/>
* <a
* href="http://code.google.com/apis/gears/geolocation_network_protocol.html" mce_href="http://code.google.com/apis/gears/geolocation_network_protocol.html">
* http://code.google.com/apis/gears/geolocation_network_protocol.html</a>
*/
public class LocationAct extends Activity {
private TextView txtInfo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnStart);
txtInfo = (TextView) findViewById(R.id.txtInfo);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
getLocation();
}
});
}
private void getLocation() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();
int cid = gsmCell.getCid();
int lac = gsmCell.getLac();
String netOperator = tm.getNetworkOperator();
int mcc = Integer.valueOf(netOperator.substring(0, 3));
int mnc = Integer.valueOf(netOperator.substring(3, 5));
JSONObject holder = new JSONObject();
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
try {
holder.put("version", "1.1.0");
holder.put("host", "maps.google.com");
holder.put("address_language", "zh_CN");
holder.put("request_address", true);
holder.put("radio_type", "gsm");
holder.put("carrier", "HTC");
data.put("cell_id", cid);
data.put("location_area_code", lac);
data.put("mobile_countyr_code", mcc);
data.put("mobile_network_code", mnc);
array.put(data);
holder.put("cell_towers", array);
} catch (JSONException e) {
e.printStackTrace();
}
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");
StringEntity stringEntity = null;
try {
stringEntity = new StringEntity(holder.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
httpPost.setEntity(stringEntity);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = null;
try {
is = httpEntity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
StringBuffer stringBuffer = new StringBuffer();
try {
String result = "";
while ((result = reader.readLine()) != null) {
stringBuffer.append(result);
}
} catch (IOException e) {
e.printStackTrace();
}
txtInfo.setText(stringBuffer.toString());
}
}
[入门阅读]怎样在android中解析JSON的更多相关文章
- 1.Android中解析json程序代码
Android程序解析json数据可以通过gson的方式,这种情况需要导入相应的jar包.测试代码如下: @Override protected void onCreate(Bundle savedI ...
- Android 中解析 JSON
有什么不懂的可以去官网去看看:www.json.org 在google android中也有关于解析JSON的类库:JsonReader,但是只能在3.0以后的版本中才可以用,在这里我们用google ...
- Android中解析JSON形式的数据
1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...
- android 中解析json格式数据
本文来自http://tonysun3544.iteye.com/category/188238 package com.tony.json; import android.app.Activity; ...
- Android中解析JSON格式数据常见方法合集
待解析的JSON格式的文件如下: [{"id":"5", "version":"1.0", "name&quo ...
- Android中解析Json数据
在开发中常常会遇到解析json的问题 在这里总结几种解析的方式: 方式一: json数据: private String jsonData = "[{\"name\":\ ...
- 实现android上解析Json格式数据功能
实现android上解析Json格式数据功能,该源码转载于安卓教程网的,http://android.662p.com ,个人感觉还不错的,大家可以看看一下吧. package com.practic ...
- android中解析文件的三种方式
android中解析文件的三种方式 好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...
- [置顶] Android学习系列-Android中解析xml(7)
Android学习系列-Android中解析xml(7) 一,概述 1,一个是DOM,它是生成一个树,有了树以后你搜索.查找都可以做. 2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件. ...
随机推荐
- 【Android】17.1 Bound Services基本概念
分类:C#.Android.VS2015: 创建日期:2016-03-03 一.Bound Services—被绑定的服务 1.什么是Bound Service Bound Service是指通过接口 ...
- android 开发 ------- 接口文档 规范
1 接口书写的格式: 1 用例图 2 流程图 3 详细的接口: 3.1请求的方式: 包含: server地址 le.gxjinan.com/open/user.php?ac=login ...
- tomcat 源码分析
Tomcat源码分析——Session管理分析(下) Tomcat源码分析——Session管理分析(上) Tomcat源码分析——请求原理分析(下) Tomcat源码分析——请 ...
- Cocos2d-x和时间有关的代码
用cocos2d-x获取系统时间,格式为年月日时分秒: void GetTime(float dt) { struct tm *tm; #if (CC_TARGET_PLATFORM == CC_PL ...
- Object.defineProperty()属性设置介绍
注释: 本文宅自https://segmentfault.com/a/1190000007434923,仅学习方便,没有任何商业用途 对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的 ...
- 請問 localtime() 為什麼不會造成 memory leak?
http://www.programmer-club.com.tw/ShowSameTitleN/vc/22380.html ************************************* ...
- php array_diff分析
前段时间和一个人聊天,聊到怎么用一个方法一次对两个数组取差集,我说使用array_diff倒是可以做到这个,但是不能只用一次,得两次.然后他就开始跟我讲他理解的array_diff的底层原理:“首先p ...
- java 多线程11:volatile关键字
直接先举一个例子普通的线程实例变量的非可见性: public class MyThread28 extends Thread { private boolean isRunning = true; p ...
- java web hello world(一)
首先在eclipse 里面创建一个java 动态项目, 记住路径,这里是直接通过根目录直接访问的webContent目录下面 的文件, 创建好后 ,在本地配置Tomcat服务器, 将server加入到 ...
- jquery ajax參数加点号状态200进error
今天开发中遇到一个非常恶心的问题: 用jquery中的ajax提交.发现假设參数中有点号的參数.后台正常返回200状态码的情况下也会走到error:function. 1. 參数data: " ...