I am trying to create an array of product SKU’s which I can then pass onto a web service.
In a text area I type say "123 456 789" I then want to pick each item from the text box and pass it using JSON so the JSON shows:
"productcode" : "123", "productcode" : "456", "productcode" : "789"
How would I go about do this please?
I have tried
var json_data = JSON.stringify({
productcode: $('#txtAddMultiProducts').val()
});
Which is returning "productcode" : "123,456,789"
You could use the JavaScript
.split()function to split the input by spaces and then construct your JSON according to the array you get in return.This will give you an array similar to this –
Now, iterating over that array, you can construct your JSON object –
You have to have unique indexes for your final JSON object otherwise each value will override the last.
This code should give you a result similar to this –