0

I have a form where a user can have multiple addresses. An address contains for example of a street and country to keep things simple.

What I have is the following in my view. Whenever a user presses the button, a different div with input pops up, which works good.

<div ng-repeat="address in addresses">
    <input type="text" ng-model="address.streetname" placeholder="streetname"/>
    <input type="text" ng-model="address.country" placeholder="country"/>
</div>
<button class="button" ng-click="addNewAddress()">Add address</button>

Part of my AngularJS controller:

var formData = {
            //...
            "addresses" : $scope.addresses
};

Spring Controller:

String[] addresses = request.getParameterValues("addresses");
for(String address : addresses) {
    System.out.println(address); //ACTUALLY PRINTS SOMETHING
    JSONObject j = new JSONObject(address); //ouch error.
    System.out.println(j.toString());
}

The following gets printed in my spring console (I have only filled in the streetname with values 'aaa', 'bbb', 'ccc'.

[{"address":"","$$hashKey":"object:4","streetname":"aaa"},{"address":"","$$hashKey":"object:6","streetname":"bbb"},{"address":"","$$hashKey":"object:8","streetname":"ccc"}]

The error message:

 A JSONObject text must begin with '{' at 1 [character 2 line 1]

Edit: Now it is working thanks to the two below, I just have to point out that there is no need to use request.getParameterValues(). Instead, just use request.getParameter("valuehere") and directly parse it to a jsonarray.

5
  • 1
    I don't know spring, but are you trying to parse and array into an object ? You should parse to an array, don't you ? Commented Apr 30, 2015 at 13:45
  • Thanks for your comment. I updated my post and parsed to an array. It is still not working unfortunately Commented Apr 30, 2015 at 13:54
  • 1
    You could try x.getJSONObject(index) instead of a simple get. Commented Apr 30, 2015 at 14:00
  • 1
    @Moody parse first into an Array then you can work with objects Commented Apr 30, 2015 at 14:08
  • Thanks for both your comments and time. It is now working. I appreciate it! Commented Apr 30, 2015 at 14:11

1 Answer 1

1

Your error is from JSONObject serialization not Spring's since you are not using @RequestBody (you might take a look here). Your json is an array which you are trying to convert into an object objects are the things between { }, thats why it's given you exception, you need a JSONArray instead

String[] addresses = request.getParameterValues("addresses");
for(String address : addresses) {
    System.out.println(address);
    JSONArray jArray = new JSONArray(address); //here!
    System.out.println(jArray.toString());
   //interate over array (like any other array) and do stuff, get the objects etc..
}

Or just change you array into an object with Angular

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.