Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'string[]' which is not a supported primitive type or a valid entity type. - How do I deserialize the json string? [duplicate]

I have below json

 "PayloadData": {
      "CustomFields": ['test','test2'],
      "SampleNumber":"123"
     }

I am using below code to deserialize the json.

   Message message = JsonConvert.DeserializeObject<Message>(payloadData);

Here is my property in Message class

    /// <summary>
    /// Gets or sets CustomFields
    /// </summary>
    [Required]
    [DataMember(Name = "CustomFields")]
    public List<string> CustomFields{ get; set; }

But, I get below error.

"The property 'Message.CustomFields' could not be mapped, because it is of type 'string[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."

like image 265
kudlatiger Avatar asked Feb 20 '19 14:02

kudlatiger


1 Answers

You can try using this online json to C# class generator to get the structure of a POCO required to deserialize a json string.

Then simply map it to your EF class (using AutoMapper for example):

So for this JSON:

 {
  "CustomFields": ['test','test2'],
  "SampleNumber":"123"
 }

this is the POCO that is generated:

public class RootObject
{
    public List<string> CustomFields { get; set; }
    public string SampleNumber { get; set; }
}

if this is your actual JSON:

    { "PayloadData":     
     {
      "CustomFields": ['test','test2'],
      "SampleNumber":"123"
     }
    }

this is how your POCO is supposed to look like:

public class PayloadData
{
    public List<string> CustomFields { get; set; }
    public string SampleNumber { get; set; }
}

public class RootObject
{
    public PayloadData PayloadData { get; set; }
}
like image 192
JustLearning Avatar answered Nov 15 '22 00:11

JustLearning