I would like to parse this string to Json:
String str="[{\"property\":\"insert_date\",\"direction\":\"ASC\"}]"
I have tried with this:
dynamic myObject=Newtonsoft.Json.JsonConvert.DeserializeObject(str)
but it returns some JArray. I would like to read values simple like:
String dir=myObject.direction;
One option is to parse string and remove square object from string. Than it would work. But i would like to do it on more proper way.
One way is to create a class and deserialize it as a List<ThatClass>
.
For example:
public class Foo
{
[JsonProperty("property")]
public string Property { get; set; }
[JsonProperty("direction")]
public string Direction { get; set; }
}
and use it like this:
var foos = JsonConvert.DeserializeObject<List<Foo>>(str);
var foo = foos.First();
Console.WriteLine(foo.Direction);
The other way is using dynamic
, and simply accessing the first element of the JArray
:
String str = "[{\"property\":\"insert_date\",\"direction\":\"ASC\"}]";
dynamic objects = JsonConvert.DeserializeObject<dynamic>(str);
Console.WriteLine(objects[0].direction);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With