客户端:
$scope.saveSelectObj = function(){ var objectArray =new Array(); var obj; for(var i=0;i<$scope.selectObject.length;i++ ) { obj = new Object(); obj.Id = $scope.selectObject[i].value; obj.Name = $scope.selectObject[i].text; objectArray.push(obj); } alert(angular.toJson(objectArray)); }
就是最后一句话 angular.toJson
服务端:
把字符串转换成 JSON对象 JSONObject.parseObject(String)
public ServiceStatus save(String userId, String json) { JSONObject jsonObject = JSONObject.parseObject(json); DashboardWidget widget = new DashboardWidget(); widget.setUserId(userId); widget.setName(jsonObject.getString("name")); widget.setData(jsonObject.getString("data")); widget.setCategoryName(jsonObject.getString("categoryName")); if (StringUtils.isEmpty(widget.getCategoryName())) { widget.setCategoryName("默认分类"); } Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("widget_name", widget.getName()); paramMap.put("user_id", widget.getUserId()); paramMap.put("category_name", widget.getCategoryName()); if (widgetDao.countExistWidgetName(paramMap) <= 0) { widgetDao.save(widget); return new ServiceStatus(ServiceStatus.Status.Success, "success"); } else { return new ServiceStatus(ServiceStatus.Status.Fail, "Duplicated name"); } }
从客户端JSON 转为服务端JSON
public ServiceStatus save(String userId, String json) { JSONObject jsonObject = JSONObject.parseObject(json); DashboardDatasource datasource = new DashboardDatasource(); datasource.setUserId(userId); datasource.setName(jsonObject.getString("name")); datasource.setType(jsonObject.getString("type")); datasource.setConfig(jsonObject.getString("config")); ...