1

I'm a little stumped on this one. Everything I do to check this out says it is a valid Json array, but JsonConvert.Deserialize says it is an object. Can someone point out what I'm doing wrong?

Code to replicate:

var data = "[{\"User\": {\"Identifier\": \"24233\",\"DisplayName\": \"Commerce Test Student\",\"EmailAddress\": \"email@email.ca\",\"OrgDefinedId\": \"UniqueId1\",\"ProfileBadgeUrl\": null,\"ProfileIdentifier\": \"zzz123\"},\"Role\": {\"Id\": 153,\"Code\": null,\"Name\": \"Commerce Student\"}}]";

var items = JsonConvert.DeserializeObject<List<T>>(data);

Where T is an object that matches the format below:

public class OrgUnitUser
{
    public User User { get; set; }
    public RoleInfo Role { get; set; }
}

public class User
{
    public string Identifier { get; set; }
    public string DisplayName { get; set; }
    public string EmailAddress { get; set; }
    public string OrgDefinedId { get; set; }
    public string ProfileBadgeUrl { get; set; }
    public string ProfileIdentifier { get; set; }
}
public class RoleInfo
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

It results in an error

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CoverPages.Models.D2L.OrgUnitUser]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Any/all help is appreciated!

Travis
  • 76
  • 1
  • 11
  • Try to remove outer square brackets `[` and `]`. – Sinatr Dec 09 '15 at 15:56
  • Removing the brackets results in the same error. Odd thing is if I copy the data variable Javascript and do a JSON.parse on it, it successfully create a javascript array of objects... – Travis Dec 09 '15 at 15:58

2 Answers2

1
var data = "[{\"User\": {\"Identifier\": \"24233\",\"DisplayName\": \"Commerce Test Student\",\"EmailAddress\": \"email@email.ca\",\"OrgDefinedId\": \"UniqueId1\",\"ProfileBadgeUrl\": null,\"ProfileIdentifier\": \"zzz123\"},\"Role\": {\"Id\": 153,\"Code\": null,\"Name\": \"Commerce Student\"}}]";

public class User
{
    public string Identifier { get; set; }
    public string DisplayName { get; set; }
    public string EmailAddress { get; set; }
    public string OrgDefinedId { get; set; }
    public object ProfileBadgeUrl { get; set; }
    public string ProfileIdentifier { get; set; }
}

public class Role
{
    public int Id { get; set; }
    public object Code { get; set; }
    public string Name { get; set; }
}

public class RootObject
{
    public User User { get; set; }
    public Role Role { get; set; }
}

var items = JsonConvert.DeserializeObject<List<RootObject>>(data);

or

var items = JsonConvert.DeserializeObject<List<RootObject>>(data)[0];

Try this code I thinks he working good

result: enter image description here

Taras Kovalenko
  • 2,323
  • 3
  • 24
  • 49
  • It looks like all you did was change Role.Code to an object and User.ProfileBadgeUrl to an object, is that correct? If so, that does not make any difference and I get the same error. – Travis Dec 09 '15 at 16:07
  • I'm updating answer with result. – Taras Kovalenko Dec 09 '15 at 16:09
  • hmm. None of the changes you made seem to have any effect since the values being passed are string and so setting it to object seems to have no effect. However, it seems the issue is with the generic. If I change List to List the deserialize is successful. – Travis Dec 09 '15 at 16:14
  • I'm not sure what your comment means. I am using DeserializeObject, but converting the data type of my objects to object from string didn't accomplish anything as the data being passed is a string and it was able to bind without the modification. – Travis Dec 09 '15 at 16:18
0

Thanks to Taras for the confirmation, but there is nothing wrong with the code itself.

When using a generic in JsonConver.Deserialize, it gives the error I listed above, however putting in the actual type of OrgUnitUser into the list, rather than T results in the convert succeeding.

Changing the code from

var items = JsonConvert.DeserializeObject<List<T>>(data);

to

var items = JsonConvert.DeserializeObject<List<OrgUnitUser>>(data);

Fixed the issue

Travis
  • 76
  • 1
  • 11
  • This isn't an answer, and should just be a comment on your question or an edit to your question – Kevin Dec 09 '15 at 16:19
  • Sorry I'm new to SO, how is it not an answer? The problem is that Newtonsoft does not handle generics in the situation I'm using the solution is that you can't use a generic. Or can it only be marked as an answer if I get an official word from newtonsoft saying it is a known glitch? I assumed it is an answer because the post above listed itself as an answer and didn't actually solve anything. I figured mine was more of an answer because I gave a reason why my code wasn't working and how I solved the issue. – Travis Dec 09 '15 at 16:21
  • @TravisSmith If it's a solution to your original question (which it looks like it is, now that you've edited it), then it qualifies as an answer. – Brian Rogers Dec 09 '15 at 16:28
  • Thanks Brian, yeah I figured maybe I didn't give enough detail about what I did to make it an "answer" so I added a little more detail. – Travis Dec 09 '15 at 16:31
  • This edited version makes it much more clear that this is an actual answer, so you can ignore my previous comment. Also, I'm going to upvote on Taras answer since it helped you get to the answer and I see that you can't upvote yet – Kevin Dec 09 '15 at 16:32