0

I am trying to deal with a task of deserializing an object into a class of my design. The string I will receive looks like this:

{nodename:"node1", version:"v1", PARM1:"p1", PARM2:"p2" ,…, PARAMN:"pn"}. 

As far as I understand, I will need a class for this json string, say Node is my class name. I know I can easily deal with nodename and version by adding two fields into my class Node. But what about the set of Params. Because the number of params in this string is dynamic and out of my control, I have no idea how to design my class. Please shed your light on this. Or do you think that is just impossible? Thanks in advance

2
  • This doesn't look like JSON, unless your example is missing the property values. Commented Sep 3, 2013 at 3:24
  • Yes, I left out the value for each property. Sorry for the confusion. @Matthew Commented Sep 3, 2013 at 3:31

2 Answers 2

1

your can use generic collections like List

    public class ResultObject 
    {
        public string NodeName { get; set; }
        public string Version { get; set; }
        public List<string> Params { get; set; }
    }

Use the above code if Params name and type is not a concern. If you want Param1, Param2,... ParamN as your parameters, then:

    public class ResultObject 
    {
        public string NodeName { get; set; }
        public string Version { get; set; }
        public Dictionary<string, string> Params { get; set; }
    }

then you can access Params with key like var x = Params["Param1"] and so on.

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

3 Comments

I think the question is more geared towards deserializing the json to an object like you suggested, not the definition of the class itself.
Matthew, you're absolutely right on this. The question is certainly not well-phased, given what I want to achieve. Thanks a lot for your answer. That is what I can get most so far if stick with the .NET standard library. -) @Matthew
Thanks for this nice piece of code, Sai. It clears my concern with the nodename and version. With Mattew's answer, I thought I needed to have the nodename and version added to the dictionary too. @Sai
0

The easiest to represent your json in code is to deserialize to a Dictionary<string, object> or Dictionary<string, string> if you know all property values are strings.

Depending on your json serializer, the syntax may differ. This is using the Microsoft JavaScriptSerializer.

var serializer = new JavaScriptSerializer();

var dict = serializer.Deserialize<Dictionary<string, string>>(jsonString);

Console.WriteLine(dict["nodename"]); // "node1"
Console.WriteLIne(dict["PARM1"]); // "p1"

You can also use the Dictionary.Keys property to get additional information all the properties in the json string.

5 Comments

Your suggestion certainly makes sense in solving the problem of unknown number of parameters. However, by directly using Dictionary, I'd have no control of parameters. By this, I mean, I need to validate the values of some of the parameters. In my case, I will only accept parameters of either string or integer type. In this case, do you think I will have to implement custom serializer?
json.net might have more flexibility out of the box, failing that, you might need to have a wrapper (or a custom serializer) for what you want to accomplish.
Considering the possibility of dealing with string or number, I am thinking of implementing a custom serializer first. Do you think I can do it with DataContractJsonSerializer or some other standard NET library classes? My best choice is to do it without third-party library.@Matthew
JSON.net is probably the most robust json serializer/deserializer, I would start there for what you want to do. james.newtonking.com/projects/json-net.aspx
JSON.net is probably my best choice now given that I could be dealing with validate my JSON string against some scheme, which is providedly exclusively by JSON.net. Thanks very much Matthew. You've been very helpful.@Matthew

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.