C# 将Json反序列化为实体框架无法将int转换为type

C# 将Json反序列化为实体框架无法将int转换为type,c#,json,entity-framework,C#,Json,Entity Framework,我一直在尝试反序列化一个Json字符串,该字符串包含一个表示颜色的int列表,然后使用Entity Framework将其插入sql数据库。我对Entity Framework非常陌生,读到它不支持基元类型的集合,我想绕过它,我可以添加这个类 public class Colors { public int Id { get; set; } public int Color { get; set; } } 然后在CharacterColor类中创建一个列表来保存

我一直在尝试反序列化一个Json字符串,该字符串包含一个表示颜色的int列表,然后使用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; }
实体

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中,颜色定义为int数组,而不是新颜色类的对象数组。应该更像颜色:[{Id:0,颜色:255},{Id:2,颜色:255},…]

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

颜色:[255255,0,0,0,0255255,0,0,0,0255255,0,0,0,0255255,0,0,0,0]

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

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

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

  • 将发送JSON的内容更改为发送{int,int}对象数组,而不是int数组
  • 将您的列表更改为列表,然后更新所有.net代码以进行调整
  • 编写一个自定义Json转换器,将Json转换为.net

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

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

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

    颜色:[255255,0,0,0,0255255,0,0,0,0255255,0,0,0,0255255,0,0,0,0]

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

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

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

  • 将发送JSON的内容更改为发送{int,int}对象数组,而不是int数组
  • 将您的列表更改为列表,然后更新所有.net代码以进行调整
  • 编写一个自定义Json转换器,将Json转换为.net

  • 你应该做1或2,因为你的数据似乎不太复杂,不足以通过3的努力。

    Hi Neil感谢你的回复,我尝试将CharacterColor类中的集合更改为数组,但仍然得到相同的错误。添加了更多细节来回答,问题不在于.net,而在于json。Hi Neil,谢谢,我将json设置为与您建议的格式相匹配,它工作得非常完美。嗨,尼尔,谢谢您的回复,我尝试将CharacterColor类中的集合更改为数组,但仍然出现相同的错误。添加了更多细节以回答问题,问题实际上不在于.net,而在于json。嗨,尼尔,谢谢,我为json添加了匹配您建议的格式的选项,它工作得非常好。在json中,
    colors
    数组是一个24个整数的列表,而在您的类中,
    colors
    是一个
    colors
    对象的列表,其中每个
    colors
    对象都有两个int属性,一个
    Id
    和一个
    Color
    。出现错误是因为Json.Net不知道如何在整数数组和对象列表之间进行转换。这可能通过
    JsonConverter
    解决,但您需要解释整数数组应该如何映射到对象中。在JSON中,
    colors
    数组是24个整数的列表,而在您的类中,
    colors
    colors
    对象的列表,其中每个
    Colors
    对象都有两个int属性,一个
    Id
    和一个
    Color
    。出现错误是因为Json.Net不知道如何在整数数组和对象列表之间进行转换。这可能通过
    JsonConverter
    解决,但您需要解释整数数组应该如何映射到对象中。
    "{\"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; }        
    }
    
     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);
            }