2

I want to create this array in C# and send it via Json to javascript. How I could write this in C#?

This is array in Javascript;

var aa =  [{
                name: 'D1',
                data: [
                [Date.UTC(2010, 0, 12, 12, 10, 10), 1],
                [Date.UTC(2010, 0, 13, 18, 01, 04), 1],
                [Date.UTC(2010, 0, 14, 14, 11, 44), 0],
                [Date.UTC(2010, 0, 15, 14, 11, 44), 1],
                [Date.UTC(2010, 0, 16, 14, 18, 44), 1],
                [Date.UTC(2010, 0, 17, 15, 11, 44), 0],
                [Date.UTC(2010, 0, 18, 16, 15, 44), 1]
             ]
            }, {
                name: 'D2',
                data: [
                [Date.UTC(2010, 0, 12), 1],
                [Date.UTC(2010, 0, 13), 0],
                [Date.UTC(2010, 0, 14), 0],
                [Date.UTC(2010, 0, 15), 0],
                [Date.UTC(2010, 0, 16), 1],
                [Date.UTC(2010, 0, 16), 0],
                [Date.UTC(2010, 0, 17), 0],
                [Date.UTC(2010, 0, 18), 0]
             ]
            }];

I tried to create the same array in c#:

public class chData
    {
        public DateTime Date { get; set; }
        public int value { get; set; }
    }

    public class finallist
    {
        public string name { get; set;} 
        public List<chData> data { get; set;} 
    }

   List<chData> D1data= new List<chData>
         { 
             new chData {Date = new DateTime(2014, 1, 1, 00,00,01) , value = 2},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,05), value = 1},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,10), value = 1},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,15), value = 0},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,20), value = 1},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,25), value = 3},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,30), value = 2},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,35), value = 1},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,40), value = 1},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,45), value = 3},
             new chData {Date = new DateTime(2014, 1, 1, 00,00,50), value = 1},
             new chData {Date = new DateTime(2014, 1, 1, 00,01,15), value = 2}

          };

 List<finallist> Finaldata= new List<finallist>();
 Finaldata.Add(new finallist { name = "D1", data = D1data});
 return JsonConvert.SerializeObject(Finaldata);

I have some issue with the structured of the array in C#, it doesn't work! I am sure I did something wrong in C# code.

this is my javascript code

var aa =[];    
$(document).ready(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/getData",
                    data: "{}",
                    dataType: "json",
                    success: function (data) {
                       aa = eval((data.d).replace(/\/Date\((.*?)\)\//gi, "new Date($1)"));
                       },
                    error: function (result) {
                        alert('#Err');
                    }
                });
            });
3
  • How is your resulting JSON array incorrect? Can you add in what your code generated? Commented Jan 22, 2014 at 14:09
  • Given that you use Date.UTC in the Javascript you might want to use the DateTime constructor that takes a DateTimeKind and specify that you want it as UTC. Commented Jan 22, 2014 at 14:13
  • yes Json array is incorrect and I think the problem is in the structure of the c# array! I am trying to build chart this is my javascript code! Commented Jan 22, 2014 at 14:15

2 Answers 2

3

To get the JSON structure you intend to, you need to change your code so that each element of the data property is an object array:

public class finallist {
  public string name { get; set; }
  public List<object[]> data { get; set; }
}

var D1data = new List<object[]>
{ 
     new object[] { new DateTime(2014, 1, 1, 00,00,05),  1},
     new object[] { new DateTime(2014, 1, 1, 00,00,10),  1},
     new object[] { new DateTime(2014, 1, 1, 00,00,15),  0},
     new object[] { new DateTime(2014, 1, 1, 00,00,20),  1},
     new object[] { new DateTime(2014, 1, 1, 00,00,25),  3},
     new object[] { new DateTime(2014, 1, 1, 00,00,30),  2},
     new object[] { new DateTime(2014, 1, 1, 00,00,35),  1},
     new object[] { new DateTime(2014, 1, 1, 00,00,40),  1},
     new object[] { new DateTime(2014, 1, 1, 00,00,45),  3},
     new object[] { new DateTime(2014, 1, 1, 00,00,50),  1},
     new object[] { new DateTime(2014, 1, 1, 00,01,15),  2}

};

List<finallist> Finaldata = new List<finallist>();
Finaldata.Add(new finallist { name = "D1", data = D1data });
return  JsonConvert.SerializeObject(Finaldata, Formatting.Indented);
Sign up to request clarification or add additional context in comments.

2 Comments

one issue I have! the date format is 2014-01-11T00:00:00 I need it to be as this format 1263297600000!! any idea how I can converted it!
you can try this: JsonConvert.SerializeObject(Finaldata, new JsonConverter[] {new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter()});
3

finallist.data should be an array of arrays:

var convertable = Finaldata.Select(f => new
{
    f.name,
    data = f.data.Select(d => new object[] { d.Date, d.value })
});

return JsonConvert.SerializeObject(convertable);

result will be equivalent to your json:

[{"name":"D1","data":[
    ["2014-01-01T00:00:01",2],
    ["2014-01-01T00:00:05",1], ...

1 Comment

Мне очень будет вас не хватать на ruSO! ☹️ И особенно как самого вменяемого модератора! Не могли бы вы, пожалуйста, рассказать почему вы удалились?

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.