0

My function returns JsonResult:

var obj = new { pc = peopleCount, fp = finalPrice, os = osoba, dt = date, zd = zadatek };
return new JsonResult { Data = obj, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

and I'd like to get all values from JsonResult. It's simple if there is only one value, but here I have an object with 5 properies. How to get them? For now I have:

var getPrice = (JsonResult)GetBundleData(item.EventID);
var price = getPrice.Data;

and I can see that in price there are those values

enter image description here

but I can't get them by just using dot and property name. There are only Equals(), GetHashCode(), GetType() and ToString().

Cezar
  • 345
  • 6
  • 18
  • Possible duplicate of [How to access JsonResult data when testing in ASP.NET MVC](http://stackoverflow.com/questions/17232470/how-to-access-jsonresult-data-when-testing-in-asp-net-mvc) – Ehsan Sajjad Mar 19 '17 at 12:29
  • there are lot of examples to do what you want http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object – Elmer Dantas Mar 19 '17 at 12:31

1 Answers1

0

since the compiler does not know the type of price at run time we can use dynamic for that purpose.

var getPrice = (JsonResult)GetBundleData(item.EventID);
dynamic price = getPrice.Data;

and use values like

DateTime date = Convert.ToDateTime(price.dt);

note that use dynamic if you are sure of properties of object else it will throw exception in runtime

let suppose if i use price.example it will build just fine without any errors but when this function is called it will throw exception because it will not find example property in price object

Usman
  • 4,615
  • 2
  • 17
  • 33