您的位置:首页 > Web前端 > JavaScript

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";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: