代码之家  ›  专栏  ›  技术社区  ›  Simant

将json反序列化为引发异常的c对象

  •  0
  • Simant  · 技术社区  · 6 年前

    你能帮我把下面的json反序列化为c吗?

    [
      {
        "detectedLanguage": {
          "language": "en",
          "score": 10.0
        },
        "translations": [
          {
            "text": "",
            "to": "da"
          },
          {
            "text": "",
            "to": "da"
          }
        ]
      }
    ]
    

    我使用了以下C类进行反序列化,但得到了一个异常。

    public class DetectedLanguage
    {
        public string language { get; set; }
        public int score { get; set; }
    }
    
    public class Translation
    {
        public string text { get; set; }
        public string to { get; set; }
    }
    
    public class RootObject
    {
        public DetectedLanguage detectedLanguage { get; set; }
        public List<Translation> translations { get; set; }
    }
    

    我的反序列化代码是:

    var response = client.SendAsync(request).Result;
    var jsonResponse = response.Content.ReadAsStringAsync().Result;
    var result = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
    

    例外

    无法将当前JSON数组(例如[1,2,3])反序列化为类型 “rootobject”,因为类型需要json对象(例如 {“name”:“value”})以正确反序列化。修正这个错误 将json更改为json对象(例如{“name”:“value”}),或更改 将类型反序列化为数组或实现集合的类型 接口(如IcLeICT,IList),像列表一样 从JSON数组反序列化。也可以添加JSONARAYAtEngy属性 强制它从json数组反序列化的类型。路径', 1号线,位置1。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Simant    6 年前

    这个 分数 属性有时包含浮点值,但在我的C类中,存在数据类型 int 这导致了异常。我以前没注意到 @ Ivan Salo 的评论。更改数据类型 int 浮动 修正了我的问题。我还使用list反序列化了@jon skeet在comments部分建议的json。

    public class DetectedLanguage
    {
        public string language { get; set; }
        public float score { get; set; }
    }
    
        2
  •  -3
  •   nmvision    6 年前

    作为完整答案编辑:

    using Newtonsoft.Json;
    
    class Program
    {
        public partial class RootObject
        {
            [JsonProperty("detectedLanguage")]
            public DetectedLanguage DetectedLanguage { get; set; }
    
            [JsonProperty("translations")]
            public Translation[] Translations { get; set; }
        }
    
        public partial class DetectedLanguage
        {
            [JsonProperty("language")]
            public string Language { get; set; }
    
            [JsonProperty("score")]
            public long Score { get; set; }
        }
    
        public partial class Translation
        {
            [JsonProperty("text")]
            public string Text { get; set; }
    
            [JsonProperty("to")]
            public string To { get; set; }
        }
    
        public partial class RootObject
        {
            public static RootObject[] FromJson(string jsonresponse) => JsonConvert.DeserializeObject<RootObject[]>(jsonresponse);
        }
    
        static void Main(string[] args)
        {
            var response = client.SendAsync(request).Result;
            var jsonResponse = response.Content.ReadAsStringAsync().Result;
            var result = RootObject.FromJson(jsonResponse);
            System.Console.WriteLine(result[0].DetectedLanguage.Language); //outputs "en"
        }
    }