2

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']
1
  • I would just write the function before the var dataset, and then the "data" key would just receive the var "result" as a parameter. Try it that way. I would also check the outcome in the console to check if the "result" array has the right format Commented Sep 19, 2018 at 7:28

4 Answers 4

1

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;
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

it showing error like Uncaught TypeError: document.getElementById(...).innerHTML is not a function
where are you executing that? That part was just taken from your sample code in order to show where the array code belongs
1

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

Comments

0

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);

Comments

0

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']

Comments

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.