[入门阅读]怎样在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文件. ...
随机推荐
- ISE和Modelsim联合仿真(详细步骤讲解)
ISE和Modelsim联合仿真(转) 地址:http://www.cnblogs.com/feitian629/archive/2013/07/13/3188192.html 相信很多人会遇到过这个 ...
- 用一个for循环实现打印乘法口诀表
本博客不再更新,很多其它精彩内容请訪问我的独立博客 循环体内定义两个变量.一个控制列,一个控制行. for(int i=0,j=0;j<9;i++){ System.out.println(j+ ...
- Java多线程面试大全
什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位,可以使用多线程对进行运算提速. 比如,如果一个线程完成一个任务要100毫秒,那么用十个线程完成改任务 ...
- 【DSP】CCS中.dat文件的格式
转自“http://e2e.ti.com/support/development_tools/code_composer_studio/f/81/t/168292” Here is the infor ...
- VC中的树形控件
树形控件可以显示出各项之间的层次关系,因此在VC中很常用. 1.树形控件的常用风格 TVS_HASBUTTONS 在父项目前显示+.-.用户可以通过点击该符号来打开或收起子项 TVS_HASLINES ...
- js屏蔽键盘按键
3.1屏蔽键盘所有键 <script language="javascript"> <!-- function document.onkeydown(){ eve ...
- pyqt布局管理器QGridLayout简单示例
# _*_ coding:utf-8 _*_ import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__ ...
- iOS网络NSURLSession使用详解
一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台.现在使用 ...
- Log4j和thymeleaf结合导致sql 日志输出不了
最近用thymeleaf模板引擎的时候,发现sql 的日志输出不了,普通ssm框架搭建没问题,能输出sql日志,可是在使用thymeleaf引擎后,就发现用不了. 现在找到一种解决方法, 导入jar包 ...
- Winform中导入exicel文件
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); ...