0

I need to convert a string to an object, just like how model binding happens in MVC controllers.
so if I have a string of json like this:

{
  "name": "tom",
  "job": "developer",
  "projects": ["projectOne", "projectTwo"]
}

I could the convert it to this object:

public class Developer
{
  public string name {get; set;}
  public string job {get; set;}
  public string[] projects {get; set;}
}

something like this:

Developer developer = Convertor.TryConvert<Developer>(myJson);

so is there any library or function that can do this?

Jonas W
  • 3,200
  • 1
  • 31
  • 44
Mohsen Shakiba
  • 1,762
  • 3
  • 22
  • 41

2 Answers2

2

Look here: How to Convert JSON object to Custom C# object?

Basically one of the answers is:

JavaScriptSerializer jss = new JavaScriptSerializer();
Developer user = jss.Deserialize<Developer>(jsonString); 
Community
  • 1
  • 1
Slava Shpitalny
  • 3,965
  • 2
  • 15
  • 22
2

You should check this out : http://www.newtonsoft.com/json

Sample :

string json = @"{
                   "name": "tom",
                   "job": "developer",
                   "projects": ["projectOne", "projectTwo"]
                 }";

Developer d = JsonConvert.DeserializeObject<Developer>(json);
Jonas W
  • 3,200
  • 1
  • 31
  • 44