JSON Object to properties

asp.net-mvc, asp.net-mvc-4, c#, json

Solution

Lets say you have the following Json

string yourJsonString = "{\"FIRST_NAME\":\"Foo\",\"LAST_NAME\":\"Bar\"}";

You could model this Json as:

public class JsonModel
{
    [JsonProperty("FIRST_NAME")]
    public string FirstName {get; set;}

    [JsonProperty("LAST_NAME")]
    public string LastName {get; set;}
}

Note that you can use `JsonPropertyAttribute` to tell `Json.Net` what the property's corresponding json field is.

Now, that you have your model set up, can use `JsonConvert.DeserializeObject<T>(...)` to get a strongly typed instance of your json model.

JsonModel jsonModel = JsonConvert.DeserializeObject<JsonModel>(yourJsonString);
string firstName = jsonModel.FirstName; // Foo
string lastName = jsonModel.LastName; // Bar

As someone had mentioned, if you do it this way, you won't have to deal with `JObject` and moreover, it will force you to actually understand the structure of the json being returned.

Specifically in your example, you could store an object of type `JsonModel` and in your model's constructor initialize it using `JsonConvert.DeserializeObject<T>`. Your public properties could then just call into that `JsonModel` instance and get the appropriate values.

Here's a more detailed example. Assume you are getting back the json I had provided above and are using the same model we created before. Let's say you have a private field in your class of type `JsonModel`

private JsonModel jsonModel;

You can initialize it in your constructor:

public UserModel()
{
    WebClient request = new WebClient();
    string response = request.DownloadString(url);
    jsonModel = JsonConvert.DeserializeObject<JsonModel>(response);
}

Now, your public properties can simply call into this `JsonModel` object.

public string FirstName
{
    get { return jsonModel.FirstName; }
    set { jsonModel.FirstName = value; }
}

Problem

I want to store the results from JSON in properties, but I don't know how. I'm using ASP.NET with MVC 4/Razor. My model's constructor is: ``` public UserModel() { WebClient request = new WebClient(); string response = request.DownloadString(url); JObject _naturalUserObject = (JObject)JsonConvert.DeserializeObject(response); } ``` And I have the follow properties: ``` [Key] public int Id { get; set; } public int DisplayName { get; set; } public int Avatar { get; set; } ``` To access a simple value from my JSON: ``` _naturalUserObject["users"][0]["name"]; ``` The final question is: how can I store each value from my JObject in my model's properties and finally display it into my view? Thanks in advance. /EDIT/ My model now is: ``` public UserModel() { WebClient request = new WebClient(); string response = request.DownloadString(String.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}", ConfigurationManager.AppSettings["SteamApiKey"].ToString(), HttpContext.Current.Request.Cookies["SteamIdCookie"].Value)); string _naturalUserObject = JsonConvert.DeserializeObject<string>(response); } ``` And one of my property is: ``` private string _avatar; public string Avatar { get { return _avatar; } set { _avatar = _naturalUserObject["response"]["players"][0]["avatar"]; } } ``` But without success. =(

Original source