Convert a string made from array toString() back to Array - javascript

I converted an array to a string and added it to a TextArea. The user edited the TextArea and I need to now update the array by calling in the same string of data I first produced. How would you do this?
The string which I have produced and need to convert back to an array is:
{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}
I tried to use the JSON Parser JSON.parse(text)

Format your string:
const text = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}'
console.log(JSON.parse(`[ ${text}]`))

You just need to format your string as an array in JSON format. You can do that like so:
JSON.parse('[' + text + ']')

The Below code should work:
var text_string = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}';
console.log(JSON.parse(`[${text_string}]`));

Related

how to add comma after each object and convert into array in javascript react js

how can i add comma after each object and convert into array. here's my response data
{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}
i want this
[{"url":"example.com/john","age":"32"},{"url":"example.com/mike","age":"42"}]
i tried with regex but it's converting data into string but i want to render this data
How about a little bit of Regex magic to make it valid JSON.
const res = '{"url":"example.com/john","age":"32"}{"url":"example.com/mike","age":"42"}';
const betterJson = `[${res.replace(/}{/g,'},{')}]`;
console.log(JSON.parse(betterJson))

JSON select from array

I have the following JSON object:
[{"_id":"57b1f03ec4f37923cc436100","pokedex_id":134,"location":{"type":"Point","coordinates":[-117.4955084,34.0175467]}},
{"_id":"57b1f03fc4f37923cc436101","pokedex_id":134,"location":{"type":"Point","coordinates":[-117.4955084,34.0175467]}}]
I'm trying to access the data with console.log(temp[0]);, but for some reason my output is just [. What am I doing wrong? Also, any subsequent index just seems to return the corresponding char value. For example: temp[1] returns {, temp[3] returns _.
In JavaScript, there is no such thing as a JSON object.
You have a JSON text, which is a string.
somestring[0] gives you the first character of that string.
Use JSON.parse() to convert your JSON text into a JavaScript array.
It's because it's stringified right now.
Accessing 'somestring'[0] will give you the first character of the string.
To cast your JSON string to a plain old JavaScript object, you should do
var actualArray = JSON.parse(temp);
Then, actualArray[0] will give you the first item in the array.
Another way is to parse your JSON file into a string, and retrieve the values.
String JSONFileString = File.ReadAllText("yourFile.json");
JavaScriptSerializer slizer = new JavaScriptSerializer();
ObjectName o1 = slizer.Deserialize<ObjectName>(JSONString);
Console.Writeline(o1);

Convert string to Json in Javascript

My string is like this :
"['01',746],['02',0],['03',9994],['04',0],['05',0],['06',0],['07',0],['08',0],['09',0],['10',0],['11',0],['12',0],['13',0],['14',0],['15',0],['16',0],['17',0],['18',0],['19',0],['20',0],['21',0],['22',0],['23',0],['24',0],['25',0],['26',0],['27',0],['28',0],['29',0],['30',0],['31',0]"
I have tried $.parseJSON() or JSON.Parse() but does not work . I am gonna use this data for google chart . so i need it in the json format .
how can i do that ?
You need to convert the single quotes to double to be valid and you would need to wrap it in [] so it is a valid array format.
var str = "['01',746],['02',0],['03',9994],['04',0],['05',0],['06',0],['07',0],['08',0],['09',0],['10',0],['11',0],['12',0],['13',0],['14',0],['15',0],['16',0],['17',0],['18',0],['19',0],['20',0],['21',0],['22',0],['23',0],['24',0],['25',0],['26',0],['27',0],['28',0],['29',0],['30',0],['31',0]";
var myArray = JSON.parse("[" + str.replace(/'/g,'"') + "]");
console.log(myArray[0][0], myArray[0][1]); // "01" 746
But a better solution is to fix what is producing that string so it is a valid JSON object to start.

convert url encoded data to JSON array or js array

How can I convert data that looks like it is url encoded to a JSON array or js array.
&nfl_s_delay=120&nfl_s_stamp=1109033755&nfl_s_left1=Cleveland%2024%20%20%20Cincinnati%203%20(END%20OF%204TH)&nfl_s_right1_count=0&nfl_s_url1=http://sports.espn.go.com/nfl/boxscore?gameId=400554328&nfl_s_left2=Kansas%20City%2017%20%20%20Buffalo%2013%20(00:00%20IN%204TH)&nfl_s_right2_count=0&nfl_s_url2=http://sports.espn.go.com/nfl/boxscore?gameId=400554332&nfl_s_left3=Miami%2016%20%20%20Detroit%2020%20(00:00%20IN%204TH)&nfl_s_right3_count=0&nfl_s_url3=http://sports.espn.go.com/nfl/boxscore?gameId=400554355&nfl_s_left4=^Dallas%2031%20%20%20Jacksonville%2017%20(FINAL)&nfl_s_right4_count=0&nfl_s_url4=http://sports.espn.go.com/nfl/boxscore?gameId=400554358&nfl_s_left5=San%20Francisco%2027%20%20%20New%20Orleans%2024%20(END%20OF%201ST%20OT)&nfl_s_right5_count=0&nfl_s_url5=http://sports.espn.go.com/nfl/boxscore?gameId=400554362&nfl_s_left6=Tennessee%207%20%20%20Baltimore%2021%20(00:00%20IN%204TH)&nfl_s_right6_count=0&nfl_s_url6=http://sports.espn.go.com/nfl/boxscore?gameId=400554367&nfl_s_left7=Pittsburgh%2013%20%20%20NY%20Jets%2020%20(END%20OF%204TH)&nfl_s_right7_count=0&nfl_s_url7=http://sports.espn.go.com/nfl/boxscore?gameId=400554370&nfl_s_left8=Atlanta%2027%20%20%20Tampa%20Bay%2017%20(END%20OF%204TH)&nfl_s_right8_count=0&nfl_s_url8=http://sports.espn.go.com/nfl/boxscore?gameId=400554372&nfl_s_left9=Denver%2041%20%20%20Oakland%2010%20(00:00%20IN%203RD)&nfl_s_right9_count=0&nfl_s_url9=http://sports.espn.go.com/nfl/boxscore?gameId=400554396&nfl_s_left10=St.%20Louis%2014%20%20%20Arizona%2010%20(00:00%20IN%202ND)&nfl_s_right10_count=0&nfl_s_url10=http://sports.espn.go.com/nfl/boxscore?gameId=400554397&nfl_s_left11=NY%20Giants%2017%20%20%20Seattle%2017%20(00:00%20IN%203RD)&nfl_s_right11_count=0&nfl_s_url11=http://sports.espn.go.com/nfl/boxscore?gameId=400554400&nfl_s_left12=Chicago%20at%20Green%20Bay%20(8:30%20PM%20ET)&nfl_s_right12_count=0&nfl_s_url12=http://sports.espn.go.com/nfl/preview?gameId=400554403&nfl_s_left13=Carolina%20at%20Philadelphia%20(8:30%20PM%20ET)&nfl_s_right13_count=0&nfl_s_url13=http://sports.espn.go.com/nfl/preview?gameId=400554408&nfl_s_count=13&nfl_s_loaded=true
I would split split by &nfl_s_left2= , but the parameter increases by one for each time. So I could split by &nfl_s_left2= but that would only split once as the next thing that is similar is &nfl_s_left3= . So I guess a more specific question is can I split by a variable string?
I could split by &nfl_s_left but then I'd get the number= in the value of the array.
What would be the best way I could clean up this data?
how about this
function url_split(str){
var jsArry={},tmp = str.split('&nfl_s_'),tmp2;
tmp.forEach(function(e){
tmp2 = e.split('=');
jsArry[tmp2[0]] = tmp2[1];
});
return jsArry;
}
and the fiddle demo http://jsfiddle.net/oswspkL0/

How to get value after a particular string in an array in javascript

I have a json data in this format.
({test:[test, "<br/><div><b>Name</b>&nbsp;&nbsp;&nbsp; James;/div><div><b>Company</b>&nbsp;&nbsp;&nbsp;Company1</div><div><b>Phone.....}).
I need to get the value after 'Company' using javascript, is there any possibilities....
Please help me on this....
assuming your data was in some sort of parsable format, you could do this:
var textAfterCompany = data.split('Company')[1];
split returns an array of strings, [0] is everything to the left and [1] is everything to the right (assuming there is only one occurrence of 'company')
var text = test[1].substring(test[1].indexOf("Company"), test[1].length);

Categories

Resources