2

I take a json object and deserialize it to a variable. When I debug, I see that each element listed like an array in Results View, but no matter what I tried, I couldn't get these values. My json string:

string json_string = { "baslik" : "bbbbb", "soru1" : "11","soru2" : "22","soru3" : "33"};

var my_object = JsonConvert.DeserializeObject(json_string);

When I debug, I see 'my_object' has Results View and Dynamic View which hold elements I need. How can I get them. I tried everything and searched in web but could not found anything.

Koray Durudogan
  • 624
  • 4
  • 12
  • 31

3 Answers3

2

You can use the dynamic variable and JObject.Parse to get values like this:

dynamic my_object = JObject.Parse(json_string);
Console.WriteLine(my_object.baslik);
Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
0

Try to do it like this:

        string json_string ="{ 'baslik' : 'bbbbb', 'soru1' : '11','soru2' : '22','soru3' : '33'}";
        var jsonObject = (JObject)JsonConvert.DeserializeObject(json_string);
        Console.WriteLine(jsonObject.GetValue("baslik"));
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
0

You could also try using JsonConvert.DeserializeObject or DeserializeAnonymousType and pass a type definition as parameter.

This will allow you to test both the structure of your data matches expectation, and you will be able to access expected properties directly off the resulting object rather than having to add a dependency on JObject.

See documentation for examples: https://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm

Chris Beiter
  • 117
  • 1
  • 10