gabed123
gabed123

Reputation: 585

.NET MVC Unable to Deserialize JSON

I am using the results from json2csharp.com on the response below from a GET request within my MVC Model:

{
  "Id": 1111111,
  "CreateLocation": "xxxxxxxxEZ-V12",
  "ConstituentType": {
    "Description": "Individual",
    "Id": 1,
    "Inactive": false,
    "ConstituentGroup": {
      "Description": "Individual",
      "Id": 1,
      "Inactive": false
    }
  },
  "DisplayName": "xxxxxxxx xxxxxxxx",
  "FirstName": "xxxxxxxx",
  "Inactive": {
    "Id": 1,
    "Description": "Active",
    "Inactive": false
  },
  "InactiveReason": null,
  "LastActivityDate": "2016-10-16T02:32:15Z",
  "LastName": "xxxxxxxx",
  "MailIndicator": {
    "Description": "(none)",
    "Id": 3,
    "Inactive": false
  },
  "EmarketIndicator": {
    "Description": "(none)",
    "Id": 3,
    "Inactive": false
  },
  "MiddleName": "",
  "NameStatus": {
    "Description": " ",
    "Id": 1,
    "Inactive": false
  },
  "OriginalSource": {
    "Description": "Test Test",
    "Id": 4,
    "Inactive": false
  },
  "PhoneIndicator": {
    "Description": "(none)",
    "Id": 3,
    "Inactive": false
  },
  "Prefix": {
    "Description": "",
    "Id": -1,
    "Inactive": false
  },
  "Gender": null,
  "Suffix": {
    "Description": "",
    "Id": -1,
    "Inactive": false
  },
  "Salutation": {
    "BusinessTitle": null,
    "CreatedDateTime": "2016-05-21T16:47:52Z",
    "CreateLocation": "xxxxxxxxEZ-V12",
    "Constituent": {
      "Id": 8775975
    },
    "CreatedBy": "xxxxxxxx",
    "Id": 912925,
    "DefaultIndicator": true,
    "EnvelopeSalutation1": "xxxxxxxx xxxxxxxx",
    "EnvelopeSalutation2": "",
    "Label": true,
    "UpdatedDateTime": "2016-05-21T16:47:52.937Z",
    "UpdatedBy": "xxxxxxxx",
    "LetterSalutation": "xxxxxxxx xxxxxxxx",
    "SalutationType": {
      "Description": "Default",
      "Id": 0,
      "Inactive": false
    },
    "EditIndicator": true,
    "IsFromAffiliation": false
  },
  "Address": {
    "Id": 9781315,
    "AffiliatedConstituent": null,
    "AltSalutationType": {
      "Description": "Default",
      "Id": 0,
      "Inactive": false
    },
    "AddressType": {
      "Description": "Home Address",
      "Id": 3,
      "Inactive": false
    },
    "City": "Brooklyn",
    "CreatedDateTime": "2016-05-21T16:47:52Z",
    "CreateLocation": "xxxxxxxxEZ-V12",
    "CreatedBy": "xxxxxxxx",
    "Constituent": {
      "Id": 8775975
    },
    "DeliveryPoint": "",
    "EndDate": null,
    "GeoArea": 35,
    "Inactive": false,
    "Label": true,
    "UpdatedDateTime": "2016-09-21T16:00:46.497Z",
    "UpdatedBy": "NCOA$STD",
    "Months": "YYYYYYYYYYYY",
    "NcoaAction": 3,
    "NcoaSession": 9,
    "PostalCode": "12121212",
    "PostalCodeFormatted": "11205-2348",
    "PrimaryIndicator": true,
    "StartDate": null,
    "State": {
      "Description": "New York",
      "StateCode": "NY",
      "Id": 51,
      "Inactive": false,
      "Country": {
        "Description": "USA",
        "Id": 1,
        "Inactive": false
      }
    },
    "Street1": "11 Fadeaddress Ave",
    "Street2": null,
    "Street3": null,
    "Country": {
      "Description": "USA",
      "Id": 1,
      "Inactive": false
    },
    "EditIndicator": true,
    "IsFromAffiliation": false
  },
  "ElectronicAddress": {
    "Address": "[email protected]",
    "AffiliatedConstituent": null,
    "AltSalutationType": null,
    "CreatedDateTime": "2016-05-21T16:47:53Z",
    "CreateLocation": "xxxxxxxxEZ-V12",
    "CreatedBy": "xxxxxxxx",
    "Constituent": {
      "Id": 8775975
    },
    "Id": 9781317,
    "ElectronicAddressType": {
      "Description": "Home Email",
      "Id": 1,
      "Inactive": false,
      "IsEmail": true
    },
    "EndDate": null,
    "AllowHtmlFormat": true,
    "Inactive": false,
    "UpdatedDateTime": "2016-05-21T16:47:53.03Z",
    "UpdatedBy": "xxxxxxxx",
    "AllowMarketing": true,
    "Months": "YYYYYYYYYYYY",
    "PrimaryIndicator": true,
    "StartDate": null,
    "EditIndicator": true,
    "IsFromAffiliation": false,
    "IsEmail": true
  },
  "PrimaryPhoneNumbers": [
    {
      "Address": {
        "Id": 9781315,
        "AddressType": {
          "Description": "Home Address",
          "Id": 3,
          "Inactive": false
        }
      },
      "AllowTelemarketing": true,
      "DayEveningIndicator": " ",
      "Constituent": {
        "Id": 8775975
      },
      "Id": 9781316,
      "Inactive": false,
      "PhoneNumber": "11111111",
      "PhoneFormatted": "(917) 561-0972     ",
      "PhoneSearch": "11111111",
      "PhoneType": {
        "Description": "Phone 1",
        "Id": 1,
        "Inactive": false
      },
      "CreatedBy": "xxxxxxxx",
      "CreateLocation": "xxxxxxxxEZ-V12",
      "CreatedDateTime": "2016-05-21T16:47:53Z",
      "UpdatedDateTime": "2016-05-21T16:47:53.027Z",
      "UpdatedBy": "xxxxxxxx",
      "EditIndicator": true
    }
  ],
  "CreatedDateTime": "2015-05-23T14:03:23Z",
  "CreatedBy": "xxxxxxxx",
  "UpdatedDateTime": "2016-10-16T02:32:15.527Z",
  "UpdatedBy": "dbo     ",
  "SortName": "xxxxxxxx/xxxxxxxx",
  "Affiliates": [],
  "ProtectionType": null
}

But when I try to deserialize the JSON output and return the results to my View with

//Storing the response details recieved from web api   
var json = Res.Content.ReadAsStringAsync().Result;

//Deserializing the response recieved from web api and storing into the Employee list  
EmpInfo = JsonConvert.DeserializeObject<List<Constituent>>(json);

I get the error Cannot deserialize the current JSON object...because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array...that can be deserialized from a JSON object.

Upvotes: 0

Views: 142

Answers (2)

Merak Marey
Merak Marey

Reputation: 799

var result = JsonConvert.DeserializeObject<dynamic>(JSONtext)

Upvotes: 0

Nat Wallbank
Nat Wallbank

Reputation: 1457

Your example response is a single object, not an array. I can't see your implementation of Constituent but assume it matches that definition?

In which case you should be able to deserialize the single result (rather than a list) using:

EmpInfo = JsonConvert.DeserializeObject<Constituent>(json);

Upvotes: 1

Related Questions