2

I have a dialog with a list of generated fields whose names contain the primary id of the user to which the data belongs. I am creating a JSON object with the data from these fields and posting it to a Spring Controller. Since the JSON does not have keys that can be matched to any specific attribute in a custom object, what type of object should I cast it to in order to most easily iterate through the object on the server side? As it is currently coded I am attempting to allow Spring to parse the object. Is there possibly a better design pattern that would be a cleaner approach?

This is my json:

({'1234567890_testID':"432342342", '0987654321_testID':"345353453"})

The value for each record will be saved for the user with the id which makes up the prefix of the key _testID. There are potentially other types of keys as well, eg: 1203048829_otherTestID.

Here's my code:

Javascript:

var $testID = $('#testDIV input');
var testData = {};
    $testID.each(function(i, el) {
    testData[$(el).attr("id")] = $(el).val();
});
var params= $.extend({ "tableID" : "testTable" }, {"testData" : testData.toSource()});

 $.ajax({ "dataType": "json",
         "type": "put",
         "url": this.url,
         "data": params,
         "success": function(alert("Success!"))
 })

Controller method signature:

@RequestMapping(value="/test", method=RequestMethod.PUT)
public @ResponseBody String updateTest(@ModelAttribute final TestCriteria tc)

ModelAttribute bean:

public class TestCriteria {
  private Map<String, Object> testData;

getters and setters...
}

Firebug parameters:

action  PUT
testData    ({'100540718367_testID':"432342342", '100540718371_testID':"252535345"})
tableID testTable
coder
  • 6,111
  • 19
  • 54
  • 73
  • You might want to post some of the Java code you have so far. Are you parsing the JSON already? If so, how? If not, you need to :) You might want to take a look at http://stackoverflow.com/questions/443499/json-to-map for some ideas. – WhiteFang34 Apr 04 '11 at 05:50

1 Answers1

0

this looks like a Map<String,Long>

Dan
  • 1,163
  • 14
  • 34
  • That looks like a good suggestion, but gives this error: Field error in object 'testCriteria' on field 'testData': rejected value [({'100540718363_testID':"353553535", '100540718371_testID':"334545657"})]; – coder Apr 04 '11 at 05:41
  • codes [typeMismatch.testCriteria.testData,typeMismatch.testData,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [testCriteria.testData,testData]; arguments []; default message [testData]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'testData';nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Map] for property 'testData': no conversion strategy found] – coder Apr 04 '11 at 05:42
  • I'm sorry, I missed the fact that the numbers are represented as Strings. You can either parse them with Long.parse(), or use Map – Dan Apr 04 '11 at 06:51
  • I received the same error when I changed the attribute to Map. It doesn't seem to like to go from String to Map. – coder Apr 04 '11 at 07:19