Convert string to json array in asp

Viewed 127

Hi i get the string value from asp and I passed the string value to json.Now I want to convert into array.Here is my code

     var dataset = [{
    "measure": "Sheet",
    "interval_s": 365 * 24 * 60 * 60,
     "data": $(function () {
        $("<%=lbl.ClientID%>").load(function () {
            var string = $( document.getElementById("<%=lbl.ClientID%>")).innerHTML();
            var splitstr = string.split(',');
            var result = "";
            for (i = 0; i < splitstr.length; i++) {
                result += (splitstr[i]);
            }
        })
    })

    }];

and the string input is

** ['2018-09-17 09:45:47',1,'2018-09-17 10:19:24'],['2018-09-17 15:53:30',1,'2018-09-17 17:35:55'],['2018-09-17 18:54:15',0,'2018-09-17 18:55:19']**

and the output i required is in array format

 ['2018-09-17 09:45:47',1,'2018-09-17 10:19:24'],
  ['2018-09-17 > 15:53:30',1,'2018-09-17 17:35:55'], 
 ['2018-09-17 18:54:15',0,'2018-09-17 18:55:19']
4 Answers

Well, this is highly dependent on how your input string looks. Assuming it always has the format of ['2018-09-17 09:45:47',1,'2018-09-17 10:19:24'],['2018-09-17 15:53:30',1,'2018-09-17 17:35:55'],['2018-09-17 18:54:15',0,'2018-09-17 18:55:19'], your string you split by does capture all the commas - even within the brackets [..,..],...

You could change the string you split by to "],[" and remove the very first character of the first array element and the very last character of the last array element.

var string = $( document.getElementById("<%=lbl.ClientID%>")).innerHTML();
var array = string.split('],[').map(function(str, index, array) {
  if(index === 0) {
    return str.substring(1);
  } else if (index === array.length -1) {
    return str.substring(0, str.length -1);
  } else {
    return str;
  }
});

You can do it by using the split function and add it to a list. given that you put the string in a variable named x. x gets your input string. so

var stringValues = "['2018-09-17 09:45:47',1,'2018-09-17 10:19:24'],['2018-09-17 15:53:30',1,'2018-09-17 17:35:55'],['2018-09-17 18:54:15',0,'2018-09-17 18:55:19']"

put it in a string array.

String[] stringValues = stringValues.Split(new string[] { "]," }, StringSplitOptions.None);

then implement this foreach logic

foreach(var item in stringValues)
    {
        var d = item + ']' + ',';
        Console.WriteLine(d);
    }

after that just remove the last 2 characters which is ],

using this code

    var finalArrayOutput = d.Remove(d.Length - 3, 3);

output will be :

['2018-09-17 09:45:47',1,'2018-09-17 10:19:24'],

['2018-09-17 15:53:30',1,'2018-09-17 17:35:55'], ['2018-09-17 18:54:15',0,'2018-09-17 18:55:19']],

here is a fiddler. click here

It looks like your data can be read as a comma delimited sequence of JSON arrays. So, enclose it in brackets and parse it.

var inputString = $( document.getElementById("<%=lbl.ClientID%>")).innerHTML();
inputString = "[" + inputString + "]";
var result = JSON.parse(inputString)
console.log(result);

You can try using Regular expression to find and remove the square brackets in the string, then splitting it by coma will return an array.

var mystring = "['2018-09-17 09:45:47',1,'2018-09-17 10:19:24'],['2018-09-17 > 15:53:30',1,'2018-09-17 17:35:55'],['2018-09-17 18:54:15',0,'2018-09-17 8:55:19']";
var myarr = mystring.replace(/\[|\]/g, "").split(",");

// output
// ['2018-09-17 09:45:47',1,'2018-09-17 10:19:24','2018-09-17 > 15:53:30',1,'2018-09-17 17:35:55','2018-09-17 18:54:15',0,'2018-09-17 8:55:19']
Related