Wouter
Wouter

Reputation: 1353

Get value of json key in razor

How to get a variable value from a json object after deserialization?

Example below:

Data

{
    "test": "test_data",
    "visible": true,
    "other": "test 2"
}

HTML

@{
    var data = Newtonsoft.Json.JsonConvert.DeserializeObject(Model.Value);

    ??? data["other"]???
}

I want to get the value that belongs to the key other.

Upvotes: 0

Views: 1521

Answers (1)

Dypso
Dypso

Reputation: 563

By using JsonConvert.DeserializeObject() you could deserialize the string as a dynamic type and then access it as usual :

var data = JsonConvert.DeserializeObject<dynamic>(Model.Value);
string other = data.other;

Hope it helps!

Upvotes: 2

Related Questions