将Json反序列化为Entity Framework无法将int转换为type

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将Json反序列化为Entity Framework无法将int转换为type相关的知识,希望对你有一定的参考价值。

我一直在尝试反序列化包含表示颜色的int列表的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。

任何对Entity Framework有更多经验的人都能看到我出错的地方。

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并用一个新的对象数组替换了colors数组

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

所以JSON是不正确的,在json中,colors数组基本上作为List发送

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期待更像下面的东西,更像是List

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

所以你可以做以下任何事情:

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

你应该做1或2,因为你的数据看起来不够复杂,无法通过3的努力。

以上是关于将Json反序列化为Entity Framework无法将int转换为type的主要内容,如果未能解决你的问题,请参考以下文章

将 JSON 反序列化为对象

将 JSON 反序列化为对象

Symfony 序列化器:将 Json 反序列化为实体

将 JSON 反序列化为匿名对象

无法将 JSON 数组反序列化为类型 - Json.NET

将 JSON 反序列化为现有对象 (Java)