博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[入门阅读]怎样在android中解析JSON
阅读量:4488 次
发布时间:2019-06-08

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

JSON入门介绍:

也参考了此篇:

json数据格式解析我自己分为两种:一种是普通的,一种是带有数组形式的:

先实例化个JSONObject对象

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()); } } 

转载于:https://www.cnblogs.com/dartagnan/archive/2011/03/09/2003450.html

你可能感兴趣的文章
2014-5-30 总结
查看>>
【H3 BPM工作流程管理产品小故事】第四篇 子表创建
查看>>
洛谷P1148 拱猪计分
查看>>
扑克序列
查看>>
java笔记--适配器模式的运用
查看>>
Replace Nested Conditional with Guard Clauses(用卫语句代替嵌套循环)
查看>>
jsp中${}是EL表达式的常规表示方式
查看>>
[Web Tools] 实用的Web开发工具
查看>>
ContentProvider
查看>>
欢迎来到Attention的博客
查看>>
获取IOS bundle中的文件
查看>>
document
查看>>
Hadoop下大矩阵乘法Version2
查看>>
iPhone内存溢出——黑白苹果
查看>>
Struts2学习笔记(十二) 类型转换(Type Conversion)(下)
查看>>
tcpdump学习
查看>>
局域网内传输文件速度慢
查看>>
Linux的核心版本(摘抄)
查看>>
CASE表达式
查看>>
zkw线段树
查看>>