【问题标题】:Deserializing Json to Entity Framework cant convert int to type将 Json 反序列化为实体框架无法将 int 转换为类型
【发布时间】:2019-05-16 06:49:29
【问题描述】:

我一直在尝试反序列化包含表示颜色的整数列表的 Json 字符串,然后使用 Entity Framework 将其插入到 sql 数据库中。我对 Entity Framework 很陌生,并且读到它不支持原始类型的集合,我想绕过它我可以添加类

public class Colors
{
    public int Id { get; set; }
    public int Color { get; set; }        
}

然后在 CharacterColor 类中创建一个列表来保存整数

public List<Colors> Colors { get; set; }

但是在尝试反序列化 Json 时出现错误。

Newtonsoft.Json.JsonSerializationException: '错误转换值 255 键入“ORAC.Data.Entities.Colors”。小路 'characterColors[0].colors[0]',第 1 行,位置 1200。 ArgumentException:无法从 System.Int64 转换或转换为 ORAC.Data.Entities.Colors。

任何对实体框架有更多经验的人都能够看到我哪里出错了。

Character character = JsonConvert.DeserializeObject<Character>(jsonString);

Json 字符串

"{\"packedRecipeType\":\"DynamicCharacterAvatar\",\"name\":\"Character\",\"race\":\"HumanMaleHighPoly\",\"dna\":[{\"dnaType\":\"UMADnaHumanoid\",\"dnaTypeHash\":-212795365,\"packedDna\":\"{\\\"height\\\":128,\\\"headSize\\\":128,\\\"headWidth\\\":93,\\\"neckThickness\\\":108,\\\"armLength\\\":135,\\\"forearmLength\\\":128,\\\"armWidth\\\":116,\\\"forearmWidth\\\":128,\\\"handsSize\\\":118,\\\"feetSize\\\":109,\\\"legSeparation\\\":128,\\\"upperMuscle\\\":129,\\\"lowerMuscle\\\":152,\\\"upperWeight\\\":128,\\\"lowerWeight\\\":81,\\\"legsSize\\\":134,\\\"belly\\\":66,\\\"waist\\\":108,\\\"gluteusSize\\\":38,\\\"earsSize\\\":121,\\\"earsPosition\\\":233,\\\"earsRotation\\\":61,\\\"noseSize\\\":115,\\\"noseCurve\\\":128,\\\"noseWidth\\\":124,\\\"noseInclination\\\":128,\\\"nosePosition\\\":128,\\\"nosePronounced\\\":128,\\\"noseFlatten\\\":118,\\\"chinSize\\\":128,\\\"chinPronounced\\\":128,\\\"chinPosition\\\":128,\\\"mandibleSize\\\":128,\\\"jawsSize\\\":128,\\\"jawsPosition\\\":128,\\\"cheekSize\\\":128,\\\"cheekPosition\\\":128,\\\"lowCheekPronounced\\\":128,\\\"lowCheekPosition\\\":195,\\\"foreheadSize\\\":128,\\\"foreheadPosition\\\":128,\\\"lipsSize\\\":128,\\\"mouthSize\\\":128,\\\"eyeRotation\\\":128,\\\"eyeSize\\\":69,\\\"breastSize\\\":128}\"},{\"dnaType\":\"UMADnaTutorial\",\"dnaTypeHash\":-1679007774,\"packedDna\":\"{\\\"eyeSpacing\\\":128}\"}],\"characterColors\":[{\"name\":\"Skin\",\"colors\":[255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{\"name\":\"Hair\",\"colors\":[255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{\"name\":\"Eyes\",\"colors\":[255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{\"name\":\"Undies\",\"colors\":[255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]}],\"wardrobeSet\":[{\"slot\":\"Underwear\",\"recipe\":\"MaleUnderwear\"}],\"raceAnimatorController\":\"Locomotion\"}"

实体

public class Character
{
    public int UserId { get; set; }
    public int CharacterId { get; set; }
    public string CharacterName { get; set; }

    public string PackedRecipeType { get; set; }
    public string Name { get; set; }
    public string Race { get; set; }
    public List<Dna> Dna { get; set; }
    public List<CharacterColor> CharacterColors { get; set; }
    public List<WardrobeSet> WardrobeSet { get; set; }        
    public string RaceAnimatorController { get; set; }

    public User User { get; set; }
}

public class Dna
{
    public int Id { get; set; }
    public string DnaType { get; set; }
    public int DnaTypeHash { get; set; }
    public string PackedDna { get; set; }
}

public class CharacterColor
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Colors> Colors { get; set; }
}

public class WardrobeSet
{
    public int Id { get; set; }
    public string Slot { get; set; }
    public string Recipe { get; set; }
}

public class Colors
{
    public int Id { get; set; }
    public int Color { get; set; }        
}

解决方案 我更新了 Json 并用新的对象数组替换了颜色数组

 JObject jsonToParse = JObject.Parse(jsonString);            
        JArray characterColors = (JArray)jsonToParse["characterColors"];

        foreach(var item in characterColors)
        {
            JArray colors = (JArray)item["colors"];
            JArray newColorsArray = new JArray();
            var i = 0;
            foreach (var col in colors)
            {
                var color = new Color
                {
                    ColorId = i,
                    Value = (int)col
                };
                newColorsArray.Add(JToken.FromObject(color));
                i++;
            }
            colors.Replace(newColorsArray);
        }

【问题讨论】:

  • 在 JSON 中,colors 数组是 24 个整数的列表,而在您的类中,ColorsColors 对象的列表,其中每个 Colors 对象有两个 int属性,IdColor。您收到错误是因为 Json.Net 不知道如何在整数数组和对象列表之间进行转换。这可以通过JsonConverter 解决,但您需要解释如何将整数数组映射到对象中。

标签: c# json entity-framework


【解决方案1】:

在您的 Json 中,您将颜色定义为整数数组,而不是新颜色类的对象数组。应该更像颜色:[{Id:0, Color:255},{Id:2, Color:255}, .......]

所以 JSON 是不正确的,在 json 中颜色数组基本上是作为列表发送的

colors: [255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]

但是 .net 期待的东西更像下面的东西,更像是一个列表

colors: [{Id:0, Color:255},{Id:1, Color:255}, ...]

因此您可以执行以下任何操作:

  1. 将发送 JSON 的内容更改为发送 {int, int} 对象数组而不是 int 数组。
  2. 将您的列表更改为列表,然后更新您的所有 .net 代码以适应它。
  3. 编写一个自定义 Json 转换器,将您拥有的 json 转换为您拥有的 .net。

您应该执行 1 或 2,因为您的数据似乎不够复杂,无法完成 3 的工作。

【讨论】:

  • 您好 Neil,感谢您的回复,我尝试将 CharacterColor 类中的集合更改为数组,但仍然出现相同的错误。
  • 添加了更多细节来回答,真正的问题不在于 .net,更多在于 json。
  • 嗨,尼尔,谢谢我添加了 json 以匹配您建议的格式,并且效果很好。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多