0

I have some textboxes generated dynamically through Ajax. I'm using Jackson 1.9.8 to parse json. I can retrieve the values of those dynamic textboxes using jQuery as follows.

var itemsArray=[];

$('input[name="txtChargeSize[]"]').each(function(){
    itemsArray[i][2]=$(this).val();                  
});

This can retrieve each element of the textbox array txtChargeSize[] one by one.

itemsArray is an array which already holds

The value of weightId on the itemsArray[i][0] position,

The value of weight on the itemsArray[i][1] position,

The value of charge on the itemsArray[i][2] position which is the value of the textbox being assigned in the above code.


I need to pass this array to Spring controller class to insert these values into the Oracle database. I'm trying the following.

var i=0;
$('input[name="txtChargeSize[]"]').each(function(){
    itemsArray[i][2]=$(this).val();        
    objectArray[i]=[["weightId",itemsArray[i][0]], ["weight",itemsArray[i][1]], ["charge",itemsArray[i][2]]];                  
    i++;          
});

It doesn't work as I expect. I need to pass something like the following.

[["weightId", 1], ["weight", 12.4], ["charge", 15.5]]

so that it can be parsed to java.util.List<Object[]>. I don't have precise knowledge of Javascript to accomplish this. How can I pass in this way the values held by itemsArray to Spring controller using json?

1 Answer 1

1
var i=0;
$('input[name="txtChargeSize[]"]').each(function(){
    itemsArray[i][2]=$(this).val();        
    i++;          
});

try javascript constructor method like this.

for(var i = 0; i < itemsArray.length; i++) {
   objectArray[i]= new createObj(itemsArray[i][0], itemsArray[i][1], itemsArray[i][2]);                  
}

and the constructor function is

function createObj(weightID, weight, charge) {
   this.weightId = weightID;
   this.weight = weight;
   this.charge = charge;
}

and finally if you want json string then use this

var str = JSON.stringify(objectArray);
alert(str);
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried it but it shows [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]. Thanks anyway for the answer.
Also tried using object notations directly like this one items={ "weightId":itemsArray[i][0], "weight":itemsArray[i][1], "charge":itemsArray[i][2] }; but it shows the same as in the previous comment.
@Tiny try JSON.stringify() method see my above answer, i have updated my it.

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.