格式一
说明:
后台通过StringBuffer拼接数据。
前台端通过struts2配置页面跳转,通过s:property标签获取对应的值(通过setter和getter方法设置对应的变量)。
{'key':[ {'key1':'value1'}, {'key2':'value2'}, ...... ] }
StringBuffer sbf = new StringBuffer(); sbf.append("{'data' : "); try{ if (null == tVisitResult) { sbf.append("'no_result'"); } else { int storeId = null == tVisitResult.getStoreId() ? 0 : tVisitResult.getStoreId(); TStore store = this.visitResultService.getById(TStore.class, storeId); sbf.append("["); // store sbf.append("{'name' : '"); sbf.append(store.getName()); sbf.append("', 'lon' : '"); sbf.append(store.getGisX()); sbf.append("', 'lat' : '"); sbf.append(store.getGisY()); sbf.append("'}, "); // check in gis sbf.append("{'name' : 'CheckIn', 'lon' : '116.45733', 'lat' : '39.91993'}, "); // check out gis sbf.append("{'name' : 'CheckOut', 'lon' : '116.46233', 'lat' : '39.95293'} "); // sbf.append("]"); } sbf.append("}"); } catch (Exception e) { e.printStackTrace(); }
{'data' : [ {'name' : '鲁春利测试店面A', 'lon' : '116.45724121462', 'lat' : '39.919978035546'}, {'name' : 'CheckIn', 'lon' : '116.45733', 'lat' : '39.91993'}, {'name' : 'CheckOut', 'lon' : '116.46233', 'lat' : '39.95293'} ] }
var data = "<s:property value='jsonData'/>"; var content = eval("("+data+")"); if ('no_store' == $.trim(result)) { return false; } for (var i in result) { if (typeof(result[i]) == "object") { if ("" == $.trim(result[i].lon)) { continue; } alert("name : " + result[i].name + "\tlon : " + result[i].lon + "\tlat : " + result[i].lat); } }
说明:
如果将
{'name' : 'CheckOut', 'lon' : '116.46233', 'lat' : '39.95293'}
格式的单引号改成双引号
{\"name\" : \"CheckOut\", \"lon\" : \"116.46233\", \"lat\" : \"39.95293\"}
的话,则解析的过程应为
var data = '<s:property value="jsonData" escape="false"/>'
格式二
JSONObject jsonObject = new JSONObject(); StringBuffer json = new StringBuffer(); for (VisitPlanInfo planInfo : this.visitPlanInfoList) { //查找已经放过的店面 FilterWrapper cancelFilter = new FilterWrapper(); cancelFilter.addFilter("companyId", userInfo.getCompanyId(), Filter.EQ); cancelFilter.addFilter("planId", planInfo.gettVisitPlan().getPlanId()); cancelFilter.addFilter("visitNo", planInfo.getVisitNo()); cancelFilter.addFilter("visitStatus", VISIT_PLAN_CANCEL, Filter.EQ); cancelFilter.addFilter("isDelete", IS_DELETE_NO, Filter.EQ); List<TVisitResult> results = this.visitResultService.getEntitiesByFilter(TVisitResult.class, cancelFilter); // json.append("'pno_"); json.append(planInfo.gettVisitPlan().getPlanId()); json.append("_"); json.append(planInfo.getVisitNo()); json.append("' : "); if (null == results || results.size() <= 0) { //全部访问完成 json.append("'no'"); } else { //有取消的拜访任务 json.append("'"); for (TVisitResult tr : results) { json.append(tr.getStoreName()); json.append("; "); } json.append("'"); } json.append(", "); } log.info("json : " + json); if (json.length() > 0) { if (json.lastIndexOf(",") > 0) { json = json.deleteCharAt(json.lastIndexOf(",")); } jsonObject.put("data", "{" + json.toString() + "}"); } else { jsonObject.put("data", "none"); } this.setJsonStores(jsonObject.toString());
{"data":{ "pno_1568_1" : "鲁春利测试店面A;", "pno_1560_1" : "鲁春利测试店面A;鲁春利测试店面B;", "pno_1420_10" : "鲁春利测试店面B;鲁春利测试店面C;", "pno_1565_1" : "鲁春利测试店面A" } }
var data = <s:property value="jsonStores"/>; //不需要在s:property 标签外添加单引号或双引号
说明:
这里不再需要通过eval("(" + + ")")来解析。如果传入某一个id值为pno_1565_1(id = pno_1565_1),则data.data[id]的值为"鲁春利测试店面A"。
格式三
过Ajax方式获取,返回类型为:setContentType("text/json;charset=UTF-8");
List<TUser> users = ...... String json = JSONArray.fromCollection(users).toString(); out.print(json);
$.post(url, null, function(data){ var content = eval("(" + data + ")"); for (i in content) { alert(content[i].name + "\t" + content[i].phoneNo); } }
web端获取到的数据: [ { "userId":10056, "name":"grady", "status":"1", "password":"grms123", "phoneNo":"12345678901", "isDelete":"0", "companyId":9 }, { "userId":10160, "name":"wangxiaojie", "status":"1", "password":"grms123", "phoneNo":"18910858581", "isDelete":"0", "companyId":9 } ]
格式四
var json = "{ 'results' : [ { 'id' : 12, 'name' : '京汉旭城家园商圈', 'shortname' : '京汉旭城...', 'boundary' : '', 'centerPoint' : '京汉旭城家园', 'lon' : '116.22391', 'lat' : '39.88617', 'shape' : '0.0', 'diameter' : '3000' },{ 'id' : 13, 'name' : '门头沟军庄中心东三小学商圈', 'shortname' : '门头沟军...', 'boundary' : '[ {\"bname\":\"门头沟军庄中心东三小学\",\"lon\":\"116.13296\",\"lat\":\"40.02321\"}, {\"bname\":\"东山贡梨园生态山庄\",\"lon\":\"116.151\",\"lat\":\"39.99931\"}, {\"bname\":\"京香花园东区西门\",\"lon\":\"116.21614\",\"lat\":\"39.97753\"} ]', 'centerPoint' : 'null', 'lon' : 'null', 'lat' : 'null', 'shape' : '0.0', 'diameter' : '0' } ] }";
var obj = eval("(" + json + ")"); var data = obj.results; for (var i in data) { if (typeof(data[i]) == "object") { if(null == data[i].boundary || "" == $.trim(data[i].boundary)) { //...... } else { // var bound = data[i].boundary; /* var k = 0; str = ''; while(bound.indexOf("\"") != -1) { k = bound.indexOf("\""); bound = bound.replace("\"", "'"); str += bound.substr(0, k + 1); bound = bound.substr(k + 1); } str += bound; */ var str = bound.replace(/\"/g, "'"); //注意正则/\"/g的形式 var jsonNew = eval("(" + str + ")"); //str为另外的JSON对象 //以上代码类似于replaceAll } } }