2

I'm forced to use GET requests to pass complex objects to my application.

How can I deserialize a querystring like this:

?people[andy]=12&people[bob]=43&people[charlie]=53&items=89&items=123&x=zulu

into a custom object like this?

public class myClass {
    public Dictionary<string, int> people { get; set; }
    public int[] items { get; set; }
    public string x { get; set; }
}

Is there a better (more sophisticated) way to do this besides splitting it by & and looping through the results to manually set each value?

Any pointers / guidance would be greatly appreciated.

Greg
  • 8,574
  • 21
  • 67
  • 109
  • Are you looking for something for sophisticated than spliting the string and setting the 'myClass' objects properties? – Glenn Ferrie Nov 25 '12 at 05:45

1 Answers1

0

If you have control over the sending side of the application, I strongly suggest you use a different encoding method to make parsing easier. I would just JSON encode the entire object, and then URIencode the JSON if it must be in the query_string.

There are often fairly small (on the order of 2K characters) limits to the size of URIs including the query_string. If you are forced to use HTTP, depending on your use case and whether you have control over this, POST may be preferable.

See this question for some methods of deserializing JSON: C# deserialize dynamic JSON

Community
  • 1
  • 1
SAJ14SAJ
  • 1,698
  • 1
  • 13
  • 32