首页 > 解决方案 > 如何将 javascript 数组放入类对象的数组列表中?

问题描述

我有以下代码:

广告类:

public class Advertisement {
    String name;
    String description;
    Date date;
    String city;
    String type;
    int rooms;
    int surface;
    int price;
    int userID;
    ArrayList<String> images;
    int httpStatus;
}

广告弹簧控制器:

 @RequestMapping(value = "/post", method = RequestMethod.POST)
    public boolean postAds(@RequestBody Advertisement newAd) {
        System.out.println(newAd.name+" "+" "+newAd.rooms+" "+newAd.surface+" "+newAd.price+" " + newAd.type+" "+newAd.description+newAd.images);

        return true;
    }

角度控制器:

$http({
  method: 'POST',
  url: AppSettings.getApiUrl('/post'),
  data: {
    name: $scope.form.name,
    description: $scope.form.description,
    //city: $scope.form.city,
    rooms: $scope.form.rooms,
    surface: $scope.form.surface,
    price: $scope.form.price,
    type: $scope.form.selected,
    images: $scope.images,
    //userId: localStorage.getItem('token'),
  }

现在,当我向服务器发出 post 请求时,它会出现以下错误:

JSON 解析错误:无法反序列化java.lang.String超出 START_ARRAY 令牌的实例;嵌套异常

我应该如何将 $scope.images 数组反序列化为图像?

标签: javascriptjavaangularjsspring

解决方案


推荐阅读