JSON解析

    // 查询在线消费记录页面
    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";
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值