0

I have a JavaScript script that makes a jQuery AJAX call, and passes a serialized javascript object in the "data" property:

data: { Specific: JSON.stringify({DAY: "1", DEP: "2", CARRIER: "3", FLT: "4", LEGCD: "5"})

It is received in a C# Generic Handler thusly:

var Specific = JsonConvert.DeserializeObject(context.Request.Params["Specific"]);

In the Generic Handler, within Visual Studio debugger, I can see the received object.

Specific = {{ "DAY": "", "DEP": "", "CARRIER": "", "FLT": "", "LEGCD": "" }}

My question is, how do I reference the received object's properties (DAY, DEP, FLT, etc)?

I tried Specific.DAY, and Specific["DAY"], with no success.

KWallace
  • 1,570
  • 1
  • 15
  • 25

2 Answers2

1

Rather than using

var Specific = JsonConvert.DeserializeObject(context.Request.Params["SpecificFlt"]);

And ending up with a type of System.Object for "Specific", It might help to deserialize to a custom type as follows:

public class SpecificObj
{
    public string DAY {get; set;}
    public string DEP {get; set;}
    public string CARRIER {get; set;}
    public string FLT {get; set;}
    public string LEGCD {get; set;}
}

And

var Specific = JsonConvert.DeserializeObject<SpecificObj>(context.Request.Params["SpecificFlt"]);

From there you should be able to access the properties using the typical dot operation (Specific.DAY)

EDIT: Alternatively you can use reflection:

Type t = Specific.GetType();
PropertyInfo p = t.GetProperty("DAY");
string day = (string)p.GetValue(Specific);

This reflection can be done other ways using newer versions of C# as detailed in one of the answers here:

How to access property of anonymous type in C#?

Matthew D
  • 196
  • 6
  • I'd rather come up with a solution that doesn't involve creating a class just for a simple data structure. Can you think of a way to just reference the object that I have? If not, then I will have to go your route. Thanks! – KWallace Mar 22 '18 at 18:12
  • @Kirby For ASP.Net MVC or Web API it is normal to create a model for complex objects that ASP.Net will automatically deserialize from JSON for you. – phuzi Mar 23 '18 at 14:20
  • Actually, in thinking about it, I went with your class example as you first suggested. I wanted to avoid flooding my App_Code folder with potentially dozens of little utility classes, scattered everywhere. BUT, I was able to add this utility class as a nested class within the generic handler itself, which is PERFECT because it's definition is right there in the class where it will be used, and everything will be self contained, and better organized. THANKS! (P.S. It worked on the first attempt, too! ;-) – KWallace Mar 23 '18 at 14:52
0

If you don't want to create the class, the following will also work

var specific = JObject.Parse(json);
// specific["DAY"] alone will return a JToken (JValue in this case),
// so use the explicit conversion to string
var day = (string)specific["DAY"];

or, if all the values are strings

var specific = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
var day = specific["DAY"]

If DAY is not present in the JSON, the first one will return null, the second one will throw KeyNotFoundException.

Rafał Rutkowski
  • 1,419
  • 10
  • 11