2

How to get the value of the dynamic object? i post the screenshot of the sample data. I'm new to c# and have no idea on object in c#.

[HttpPost]
[Route("api/TCSAPI/SaveTCS")]
public HttpResponseMessage SaveTCS(dynamic tcsObject)
{
    var track = tcsObject.GetType().GetProperty("track").GetValue(tcsObject, null);
    return Helper.ComposeResponse(HttpStatusCode.OK, string.Empty);
}

enter image description here

Update: javascript console. this is the data i sent to the API

enter image description here

Update: i can now get the type as what Abion47 told and it is a JObject. how to get the data of this object?

  • the incoming value is already dynamic. no need for reflection. `var track = tcsObject.track` – Nkosi Jan 12 '17 at 11:10

1 Answers1

2

You just get it. Treat it as if it were the type you are expecting it to be, and if it doesn't work, it will throw an error. That's how dynamic works, by imitating the behavior of weakly-typed languages.

var track = tcsObject.track;
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • yeah your right. how about the category? –  Jan 12 '17 at 11:16
  • @SecretCoding What category? If it's another field/property, you get it in the exact same way: `tcsObject.category` – Abion47 Jan 12 '17 at 11:18
  • the one in the image. the tcsObject has a category which is a category consist of many objects with array in javascript. when i do the tcsOject.category.Value i got null. how to convert it to List or in C# datatype? –  Jan 12 '17 at 11:19
  • @SecretCoding That depends on what the type of `category` is. Although looking at the image it doesn't look like your object has `track` and `category` properties at all, but rather is an object representing JSON data. What is the actual type of `tcsObject`? – Abion47 Jan 12 '17 at 11:22
  • please see the updated image @Abion47 –  Jan 12 '17 at 11:27
  • @SecretCoding Like I said, those are not fields that the `tcsObject` object has, necessarily. Those are fields that are within JSON data that `tcsObject` apparently wrappers. You are trying to get the JSON data by treating it as if the object itself has that field, but looking at your image of Visual Studio the object doesn't have that field. Instead it has `ChildrenTokens`, `Count`, `First`, `HasValues`, etc. This looks like an object for traversing JSON data, not an object with the actual fields you are trying to access. – Abion47 Jan 12 '17 at 11:32
  • what should i do then??? –  Jan 12 '17 at 11:35
  • @SecretCoding It depends on what type `tcsObject` actually is. If you did `Console.WriteLine(tcsObject.GetType());`, what prints? – Abion47 Jan 12 '17 at 11:36
  • `tcsObject.GetType()` will return null –  Jan 12 '17 at 11:41
  • @tcsObject So there's an issue in what you're doing. `tcsObject.GetType()` should _never_ return null. In fact, nothing will ever return null when you call `GetType()` on it. At the very least, it will return `System.Object`. Even if `tcsObject` was null, calling `GetType()` on it would throw an exception. So either you are doing something very weird or you are misreading your data. – Abion47 Jan 12 '17 at 11:46
  • i dont know why but when i do the that you answer `var track = tcsObject.track;` and add `.Value` it still gets the data. except for the category –  Jan 12 '17 at 11:55
  • @SecretCoding That's the thing, though. None of the information you are giving me tells me why `.Value` would work; the Visual Studio image doesn't reveal any property called `Value`. I can't say any more than I have without knowing what type we are working with. – Abion47 Jan 12 '17 at 11:59