C# Entity Framework Json deserialize String array issues

c# deserialization entity-framework entity-framework-6 json

Question

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.

1
0
1/22/2019 6:30:10 PM

Popular Answer

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.

0
1/25/2019 11:21:37 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow