简体   繁体   中英

C# Entity Framework Json deserialize String array issues

I have A Json file Which can be used for deserialize to Entity framework. For simplify we can assume the Json like this

{
  "stat": "val0",
  "results": [
    {
      "datasets": [
        "val1",
        "val2"
      ],
      "head": "val3"
    },
    {
      "datasets": [
        "val4",
        "val5"
      ],
      "head": "val6"
    }
  ]
} 

And my Entity Classes like

[Serializable]
public class Root
{
    [Key]
    public int Id { get; set; }
    public int stat { get; set; }
    public List<Result> results { get; set; }
}

[Serializable]
public class Result
{
    [Key]
    public int Id { get; set; }
    public List<String> _strings { get; set; }
    public List<string> Strings
    {
        get { return _strings; }
        set { _strings = value; }
    }

    [Required]
    public string datasets
    {
        get { return String.Join(",", _strings); }
        set { _strings = value.Split(',').ToList(); }
    }
    public string head{ get; set; }
    public virtual root { get; set; }

}

I know Entity Framework does not support primitive types and I know problem causes from my datasets fields. that I found this way to solve String array deserialize issue here . I have tried

 URL = "http://...";//Restful webservice address
 WebClient client = new WebClient();
 String JSON= client.DownloadString(URL);
 var dsobj = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<RootObject>(json);

But I got

System.InvalidOperationException

Then I have decided to use Newtonsoft

URL = "http://...";//Restful webservice address
WebClient client = new WebClient();
String JSON= client.DownloadString(URL);
var dsobj = JsonConvert.DeserializeObject<Root>(json);

Then I got this error

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Path 'results[0].senses[0].definition', line 1, position...

I found this but I cant figure it out.

How can Fix these isseus. Any help appreciated.

Your json consist of two unwanted commas, try removing those

在此处输入图片说明

try

[Serializable]
public class Root
{
    [Key]
    public int Id { get; set; }
    public string stat { get; set; } // changed to a string
    public List<Result> results { get; set; }
}

[Serializable]
public class Result
{
    [Key]
    public int Id { get; set; }
    public List<String> _dataSets { get; set; }
    public List<string> dataSets // the JSON array will deserialize into this property
    {
        get { return _dataSets; }
        set { _dataSets = value; }
    }

    [Required]
    public string DatasetsAsString
    {
        get { return String.Join(",", _dataSets); }
        set { _dataSets = value.Split(',').ToList(); }
    }
    public string head{ get; set; }
    public virtual root { get; set; }

}

Edit: stat property has to be a string too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM