1

I'm trying to deserialize my string here

LicenseName=10&StartDate=7%2F20%2F2016&EndDate=09%2F28%2F2016

using this method

var serializer = new JavaScriptSerializer();
        ConsumedUserLicense jsonObject = serializer.Deserialize<ConsumedUserLicense>(licenseDetails);

to this object

public class ConsumedUserLicense
{
    public int LicenseId { get; set; }
    public string LicenseName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

but I'm getting an error that says

"Invalid JSON primitive: LicenseName.

chuckd
  • 13,460
  • 29
  • 152
  • 331
  • 1
    That string is not valid JSON. It's not even *invalid* JSON. Where did you get it from? It's a query string, isn't it? Come on, fess up. We're all friends here. – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 19:52
  • I used $('#form).serialize() in Javascript. What should I have done? – chuckd Jul 20 '16 at 20:01
  • Yeah, that method returns a properly encoded query string (as in a URL), not JSON. There's no direct way to serialize a form to JSON in JS out of the box, but [this answer](http://stackoverflow.com/a/1184667/424129) might do it. I haven't tested it, mind you, and right now they're howling at me to fill out some idiotic form for human resources. – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 20:03
  • ok, so how do I deserialize a query string in c#??? – chuckd Jul 20 '16 at 20:06
  • a) use json instead. b) paste that exact question into Google and click on the StackOverflow links at the top of the page until you see one that works. I gotta run, they'll screw up my dental coverage and I'll look like Shane MacGowan. – 15ee8f99-57ff-4f92-890c-b56153 Jul 20 '16 at 20:07

1 Answers1

0

Your JSON-String is not a correct JSON-String. Try to copy-paste it in a JSON validator to check it out: JSON Formatter and Validator.

From json.org:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

See also: json.org

danny
  • 358
  • 2
  • 14