How to insert text into json array using jquery/javascript .
supposing I have data as
some numbercodes in a text file format 123, 456, 789
I want to get them in a json array format using javascript/jquery.
var nuumbercodes = [ "123","456","789" ];
How to insert text into json array using jquery/javascript .
supposing I have data as
some numbercodes in a text file format 123, 456, 789
I want to get them in a json array format using javascript/jquery.
var nuumbercodes = [ "123","456","789" ];
If you have a well formatted text string with comma separated numbers,
like this '123,456,789'
This should have no spaces or tabs,
Then you can convert it simply into a JavaScript array.
var myTextwithNuumbercodes='123,456,789';
var numbercodes=myTextwithNuumbercodes.split(',');
returns ['123','456','789']
if you have a JSON string like this '[123,456,789]' then you get a javascript array by calling JSON.parse(theJSONString)
var numbercodes=JSON.parse('[123,456,789]');
returns [123,456,789]
notice the "[]" in the string ... that is how you pass a JSON array
toconvert it back to a string you can use JSON.stringify(numbercodes);
if you have a total messed up text then it's hard to convert it into a javascript array but you can try with something like that
var numbercodes='123, 456, 789'.replace(/\s+/g,'').split(',');
this firstly removes the spaces between the numbers and commas and then splits it into a javascript array
in the first and last case you get a array of strings u can transform this strings into numbers by simply adding a + infront of them if you call them like
mynumbercode0=(+numbercodes[0]);// () not needed here ...
in the 2nd case you get numbers
if you want to convert an array to a string you can also use join();
[123,456,789].join(', ');
Assuming your data is in a string, then split it on commas, use parseInt in a for loop to convert the string numbers into actual Numbers and remove the whitespace, then JSON.stringify to convert to JSON.
You could use .push() push values at the end of an array. After that you could use JSON.stringify(nuumbercodes) to make a JSON string representation of your Array.