【问题标题】:C# Entity Framework Json deserialize String array issuesC# Entity Framework Json 反序列化字符串数组问题
【发布时间】:2019-06-16 06:06:46
【问题描述】:

我有一个可用于反序列化为实体框架的 Json 文件。为了简化,我们可以假设 Json 是这样的

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

还有我的实体类,比如

[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; }

}

我知道 Entity Framework 不支持原始类型,并且我从我的数据集字段中知道问题的原因。我发现这种方法可以解决字符串数组反序列化问题here。我试过了

 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);

但我得到了

System.InvalidOperationException

那我决定用Newtonsoft

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

然后我得到了这个错误

Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符: [.路径 'results[0].senses[0].definition',第 1 行,位置...

我找到了this,但我想不通。

如何修复这些问题。任何帮助表示赞赏。

【问题讨论】:

  • datasets JSON 数组应与实体类中的 List&lt;string&gt; 匹配。试试public List&lt;string&gt; dataSetspublic string datasetsAsString
  • 我听不懂@michaelyin 你能告诉我吗?

标签: c# json entity-framework entity-framework-6 deserialization


【解决方案1】:

您的 json 包含两个不需要的逗号,请尝试删除它们

【讨论】:

  • 当我简化原始 json 时,我忘记了清除逗号。然后我编辑了json。
【解决方案2】:

试试

[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; }

}

编辑:stat 属性也必须是字符串。

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多