0

this is javascript code, I want to change it into C# code, same as this is, b/c again I'll change it to javascript object

data: [['2-2010',45.0],['IE', 26.8],[ 'Chrome',12.8],['Safari',8.5],['Opera',6.2],['Others', 0.7]]

actually I'm writing a wrapper which will take values in C# language and then through json serialize I'll convert the code into json.

I can't do it like this, b/c at the time of creating json it would be some thing like

C#

class dataArray
{
  public string browserId;
  public double percentRate;
}

JS Generated by the above class but not useable for me b/c of the variable browser and percentRate

dataArray = {browser: 'chrome', 'percentRate':30.3}

I was expecting something like this List<string,double> but it would never work :D

3 Answers 3

2

You need a List of object arrays to get the output that you're looking for. I used JSON.net for the example code below.

class Program
{
    static void Main(string[] args)
    {
        List<object[]> kvp = new List<object[]>()
        {
            new object[] {"2-2010", 45},
            new object[] {"IE", 26.8},
            new object[] {"Chrome", 12.8},
            new object[] {"Safari", 8.5}
        };

        var json = JsonConvert.SerializeObject(kvp);

        Console.WriteLine(json);

        //deserialize it to a List<object[]>
        var json2 = "[[\"2-2010\",45.0],[\"IE\", 26.8],[\"Chrome\",12.8],[\"Safari\",8.5]]";
        var kvp2 = JsonConvert.DeserializeObject<List<object[]>>(json2);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

No, you'd better have an array of dictionaries, each dictionary will be equal to the object and its key and values will be his property and value

1 Comment

Why do you advocate an array of Dictionaries rather than simply a Dictionary<string, float>?
0

You could use anonymous types which looks a lot like the JS code, but without single quoutes around percentRate. You could also use Tuples, which is Tuple<string, float> and is basically a pair of values. Multidimensional or jagged arrays are another option.

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.