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.

2
  • I am not sure of the significance of the double curlys "{{" and "}}". Does it have something to do with it? Commented Mar 22, 2018 at 15:22
  • Why not just do data: { SpecificFit: { DAY: "1", DEP: "2" } } rather than stringifying the inner object? Commented Mar 22, 2018 at 21:37

2 Answers 2

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#?

Sign up to request clarification or add additional context in comments.

3 Comments

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!
@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.
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! ;-)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.