JSON解析
2016-03-01 11:35
399 查看
// 查询在线消费记录页面 public void getTicketConsumption(final Handler handler, final String deviceId) { // 获取ShowPayCashRecordActivity的数组 ShowOnlineConsumeRecordActivity.listData = new ArrayList<HashMap<String, Object>>(); final String url = "http://202.173.255.26:9995/HDServices.asmx/Get_Ticket_Consumption"; httpCilent = new DefaultHttpClient(); new Thread(new Runnable() { @Override public void run() { // 设置请求的路径 httpPost = new HttpPost(url); try { final List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("deviceid", deviceId)); // 发送参数到服务器 httpPost.setEntity(new UrlEncodedFormEntity(params)); response = httpCilent.execute(httpPost); // 处理服务器返回的JSON数据 final int statuCode = response.getStatusLine().getStatusCode(); if (statuCode != HttpStatus.SC_OK) { try { throw new Exception(); } catch (final Exception e) { } } else { // 获取服务器返回Response实体 entity = response.getEntity(); final String results = EntityUtils.toString(entity, "GB2312"); // 把返回结果转化成jsonobject final JSONObject obj = new JSONObject(results); // 解析返回的json数据 errmsg = obj.getString("Errmsg"); errcode = obj.getString("Errcode"); // 获取json数组,里面可能有多个记录 final JSONArray consumptions = obj.getJSONArray("TicketConsumptions"); final int consumptionsCount = consumptions.length(); if (errmsg.equals("ok") && errcode.equals("0")) { for (int i = 0; i < consumptionsCount; i++) { // 拿到每一个在线消费对象 final JSONObject consumption = consumptions.getJSONObject(i); final String sPrice = consumption.getString("SPrice"); final String spriceTrue = consumption.getString("SpriceTrue"); final String serviceName = consumption.getString("ServiceName"); final String serverContent = consumption.getString("ServerContent"); final String userTime = consumption.getString("userTime"); final HashMap<String, Object> map = new HashMap<String, Object>(); map.put("denomination", sPrice); map.put("amountPayCash", spriceTrue); map.put("serviceNameRecord", serviceName); map.put("serviceContentRecord", serverContent); map.put("validateTime", userTime); ShowOnlineConsumeRecordActivity.listData.add(map); } // 发送成功信息 final Message msg = handler.obtainMessage(); msg.what = MessageContent.MSG_SUCCESS; handler.sendMessage(msg); } else if (Integer.parseInt(errcode)<0){/*else if (errcode.equals("-9001") || errcode.equals("-9004")) {*/ // 发送失败信息 final Message msg = handler.obtainMessage(); msg.what = MessageContent.MSG_ERROR; handler.sendMessage(msg); } } } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } catch (final ClientProtocolException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final JSONException e) { e.printStackTrace(); System.out.println("这里有exception" + e.toString()); } } }).start(); }
// 点击输入验证消费密码页面的确认按钮 public void ticketValid(final Handler handler, final String deviceId, final String pwd) { public static HashMap<String, String> ticketValidHashMap; ticketValidHashMap = new HashMap<String, String>(); final String url = "http://202.173.255.26:9995/HDServices.asmx/TicketValid"; httpCilent = new DefaultHttpClient(); new Thread(new Runnable() { @Override public void run() { // 设置请求的路径 httpPost = new HttpPost(url); try { final List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("deviceid", deviceId)); params.add(new BasicNameValuePair("cardno", null)); params.add(new BasicNameValuePair("spPasswd", pwd)); // 发送参数到服务器 httpPost.setEntity(new UrlEncodedFormEntity(params)); response = httpCilent.execute(httpPost); // 处理服务器返回的JSON数据 final int statuCode = response.getStatusLine().getStatusCode(); if (statuCode != HttpStatus.SC_OK) { try { throw new Exception(); } catch (final Exception e) { } } else { // 获取服务器返回Response实体 entity = response.getEntity(); final String results = EntityUtils.toString(entity, "GB2312"); // 把返回结果转化成jsonobject final JSONObject obj = new JSONObject(results); // 解析返回的json数据 errmsg = obj.getString("Errmsg"); errcode = obj.getString("Errcode"); if (errmsg.equals("ok") && errcode.equals("0")) { // 解析json获取当前积分、汇率、步长的值 final String spCode = obj.getJSONObject("Ticket").getString("SPcode"); final String sPrice = obj.getJSONObject("Ticket").getString("SPrice"); final String cNameShort = obj.getJSONObject("Ticket").getString("Cnameshort"); final String serviceName = obj.getJSONObject("Ticket").getString("ServiceName"); final String serverContent = obj.getJSONObject("Ticket").getString("ServerContent"); ticketValidHashMap.put("spCode", spCode); ticketValidHashMap.put("sPrice", sPrice); ticketValidHashMap.put("cNameShort", cNameShort); ticketValidHashMap.put("serviceName", serviceName); ticketValidHashMap.put("serverContent", serverContent); // 发送成功信息 final Message msg = handler.obtainMessage(); msg.what = MessageContent.MSG_SUCCESS; handler.sendMessage(msg); } else if (Integer.parseInt(errcode)<0){/*else if (errcode.equals("-9002") || errcode.equals("-1019") || errcode.equals("-9001") || errcode.equals("-9003") || errcode.equals("-1003") || errcode.equals("-1007") || errcode.equals("-1008") || errcode.equals("-1014") || errcode.equals("-1015") || errcode.equals("-1016") || errcode.equals("-1017") || errcode.equals("-1018")) {*/ // 发送失败信息 final Message msg = handler.obtainMessage(); msg.what = MessageContent.MSG_ERROR; handler.sendMessage(msg); } } } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } catch (final ClientProtocolException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final JSONException e) { e.printStackTrace(); System.out.println("这里有exception" + e.toString()); } } }).start(); }
var employees = [ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName": "Carter" } ]; 可以像这样访问 JavaScript 对象数组中的第一项:employees[0].lastName;返回结果是:Gates; 可以直接修改数据:employees[0].lastName = "Jobs";
相关文章推荐
- 使用live delegate on来解决js对后加载的html失效的问题
- 了解学习JS中this的指向
- JS两个等号和三个等号
- JavaScript实现简洁的俄罗斯方块完整实例
- javascript实例学习之二——类新浪微博的输入框
- JS的内建函数reduce
- js 回调函数
- html + js 右 点击 弹出 菜单
- 几个JS DOM操作的函数
- extjs 初始化函数 中的 this.callParent(arguments);类似于继承么?就是继承父类的对应的初始化函数
- Web与JS交互一个强大的方法
- JavaScript跨域总结
- 10款面向HTML5 画布(Canvas)的JavaScript库
- 第一个JSP程序
- JS实现图片平面旋转的方法
- <a>标签中href="javascript:;"的意思
- js关闭弹出window
- JavaScript如何获取css属性
- 20160301--Js01
- JavaScript脚本,ie允许阻止的内容