How can I convert javascript var obj = {}; to C#?

Viewed 94

I have the following javascript code:

var key = "Mykey" + NextNumber.toString();
var value = {"Name":"Tony","Width":"150","Height":"320"};
var valuejson = JSON.stringify(value);
var obj = {};
obj[key] = valuejson

I know how to create valuejson in C#, but I don't know how to create something similar like var obj = {}; in C#. How can I do that in C#?

key and valuejson in C#:

 public class MyValue
 {
     public string Name { get; set; }
     public string Width { get; set; }
     public string Height { get; set; }
 }

 MyValue value = new MyValue();
 value.Name = "Tony";
 value.Width = "150";
 value.Height = "320";
 string jsonValue = JsonConvert.SerializeObject(value);
 string key = "Mykey" + NextNumber.toString(); 
4 Answers

If you're looking for an object whose members can be dynamically added and removed at run time (like in Javascript) then the ExpandoObject class should fit your needs.

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;

foreach (var property in (IDictionary<String, Object>)employee)
{
    Console.WriteLine(property.Key + ": " + property.Value);
}
// This code example produces the following output:
// Name: John Smith
// Age: 33

You could try to use a dynamic object which works similar to {} in javascript... But... You have to be CAREFUL, example:

public class test 
{
    public class MyValue
    {
        public string Name { get; set; }
        public string Width { get; set; }
        public string Height { get; set; }
    }

    public void testing() 
    {
        MyValue value = new MyValue();
        value.Name = "Tony";
        value.Width = "150";
        value.Height = "320";
        dynamic jsonValue = new { keyA = value };
        string height = jsonValue.keyA.Height;
    }
}

EDI:

Actually, I read a bit more carefully your need, and a dictionary can also suit your needs:

public class test
{
    public class MyValue
    {
        public string Name { get; set; }
        public string Width { get; set; }
        public string Height { get; set; }
    }

    Dictionary<string, MyValue> dic = new Dictionary<string, MyValue>();
    public void testing()
    {
        string key = "Mykey" + NextNumber.toString();

        MyValue value = new MyValue();
        value.Name = "Tony";
        value.Width = "150";
        value.Height = "320";

        dic.Add(key, value);
    }
}

Since you where asking for the equivalent of a var x = {} I suggested the dynamic but I see you want to create a key and associate that value to that key.

To convert from a json string to a C# object, you can use the DeserializeObject method and pass in an instance of the c# object that you want to convert this json to. The JsonConvert library is part of the Newtonsoft.Json library.

 var converted = JsonConvert.DeserializeObject<myCSharpViewModel>(json);

You are asking for dynamic ExpandoObject.

Example:

dynamic obj = new ExpandoObject();

obj.banana = "CIAO";
obj.bidshmqwq = 11245;
obj.BRUFCJMWH = null;

In substance, this Type can let you declare object's properties dynamically.

Related