【问题标题】:Deserializing JSON data to C#将 JSON 数据反序列化为 C#
【发布时间】:2017-06-12 10:34:58
【问题描述】:

你能帮我将 JSON 反序列化为 C# 吗?
我只是在上面吃我的牙齿.. =.=.

我找到了很多方法来解决这个 xD,
我不想与 U 分享。
我希望等待您的建议。

有用的链接:
http://jsonviewer.stack.hu/
http://json2csharp.com/

JSON看起来..

[
   {
      "faceId":"626f5974-1d63-40d4-98f1-7e6a7df13dba",
      "faceRectangle":{
         "top":108,
         "left":699,
         "width":208,
         "height":208
      },
      "faceAttributes":{
         "smile":0.973,
         "gender":"male",
         "age":25.7,
         "emotion":{
            "anger":0.0,
            "contempt":0.026,
            "disgust":0.0,
            "fear":0.0,
            "happiness":0.973,
            "neutral":0.001,
            "sadness":0.0,
            "surprise":0.0
         }
      }
   },
   {
      "faceId":"bc051f1d-9a64-4e86-bf95-2af1de21d316",
      "faceRectangle":{
         "top":104,
         "left":634,
         "width":114,
         "height":114
      },
      "faceAttributes":{
         "smile":0.074,
         "gender":"male",
         "age":17.4,
         "emotion":{
            "anger":0.003,
            "contempt":0.003,
            "disgust":0.001,
            "fear":0.002,
            "happiness":0.074,
            "neutral":0.828,
            "sadness":0.079,
            "surprise":0.01
         }
      }
   }
]

【问题讨论】:

  • 到目前为止你尝试过什么?您想将其反序列化为哪些 C# 对象?你没有给我们太多继续前进的机会。
  • 我不想与 U 分享。 恐怕它不会那样工作。你需要分享你尝试过的东西,并告诉我们它有什么问题。然后我们将能够为您提供帮助。您还应该阅读这些主题stackoverflow.com/help/asking
  • 不明白你想要什么:)))
  • 为我的能力不足道歉,但 Rufo 爵士理解我;3 我说:你能帮我将 JSON 反序列化为 C# 吗?所以他比我更希望将 JSON 反序列化为一些智能类,他向我展示了如何访问对象;) - 我很感激。 @Guy 我说:我不想和 U 分享,因为我为自己的涂鸦感到羞耻。感谢你在 stacksociety 的欢迎;3 #Cold_shower

标签: c# json deserialization


【解决方案1】:

首先,您需要要反序列化的类,因此您可以手动创建或最快和最简单的方法 - 您可以复制您拥有的 json 字符串,然后在 Visual Studio 中 p>

编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类

现在你有了反序列化的结构。然后你可以使用Newtonsoft.Json(你可以从 NuGet 下载)。有JsonConvert.DeserializeObject<T>() 泛型方法,它将为您完成所有反序列化工作。

另外,如果你想在class结构中使用你自己的属性名,你可以使用[JsonProperty("Name")]属性,当属性被序列化为JSON时,它会改变属性名,反之亦然。

【讨论】:

  • 哇。谢谢,很高兴知道 ;) 每天我都变得更好 ;) 最好的问候
【解决方案2】:

您的 JSON 文档代表一个 array,因此您必须将其反序列化为一个集合

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string jsonString = "[ {\"faceId\": \"626f5974-1d63-40d4-98f1-7e6a7df13dba\",\"faceRectangle\": {  \"top\": 108,  \"left\": 699,  \"width\": 208,  \"height\": 208     },\"faceAttributes\": {  \"smile\": 0.973,  \"gender\": \"male\",  \"age\": 25.7,  \"emotion\": {    \"anger\": 0.0,    \"contempt\": 0.026,    \"disgust\": 0.0,    \"fear\": 0.0,    \"happiness\": 0.973,    \"neutral\": 0.001,    \"sadness\": 0.0,    \"surprise\": 0.0        }    }},{\"faceId\": \"bc051f1d-9a64-4e86-bf95-2af1de21d316\",\"faceRectangle\": {  \"top\": 104,  \"left\": 634,  \"width\": 114,  \"height\": 114     },\"faceAttributes\": {  \"smile\": 0.074,  \"gender\": \"male\",  \"age\": 17.4,  \"emotion\": {    \"anger\": 0.003,    \"contempt\": 0.003,    \"disgust\": 0.001,    \"fear\": 0.002,    \"happiness\": 0.074,    \"neutral\": 0.828,    \"sadness\": 0.079,    \"surprise\": 0.01        }    }}]";
        var result = JsonConvert.DeserializeObject<IList<RootObject>>(jsonString);

        foreach ( var item in result )
            Console.WriteLine( item.faceId );
    }
}

// Generated by http://json2csharp.com

public class FaceRectangle
{
    public int top { get; set; }
    public int left { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public double anger { get; set; }
    public double contempt { get; set; }
    public double disgust { get; set; }
    public double fear { get; set; }
    public double happiness { get; set; }
    public double neutral { get; set; }
    public double sadness { get; set; }
    public double surprise { get; set; }
}

public class FaceAttributes
{
    public double smile { get; set; }
    public string gender { get; set; }
    public double age { get; set; }
    public Emotion emotion { get; set; }
}

public class RootObject
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

.net fiddle

【讨论】:

  • 你做的有多快......你解决这个问题的速度有多快...... -.-因为你得到了午餐刹车,所以你手里拿着三明治检查stackoverflow,你做这个xd......谢谢伴侣。之前:我尝试只使用一些数组的 faceRectangle ..,我替换了一些字符 { => [ 等等再次
【解决方案3】:

如果您想要来自该 JSON 的 c# 对象,可以这样做:

public class FaceRectangle
{
    public int top { get; set; }
    public int left { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public double anger { get; set; }
    public double contempt { get; set; }
    public double disgust { get; set; }
    public double fear { get; set; }
    public double happiness { get; set; }
    public double neutral { get; set; }
    public double sadness { get; set; }
    public double surprise { get; set; }
}

public class FaceAttributes
{
    public double smile { get; set; }
    public string gender { get; set; }
    public double age { get; set; }
    public Emotion emotion { get; set; }
}

public class RootObject
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

var obj = JsonConvert.DeserializeObject<RootObject>(json);

【讨论】:

    【解决方案4】:

    我建议您自己生成这样一个类,而不是给您一个映射到 JSON 的 C# 类,而您只是盲目地复制它。

    1. 确保您运行的是 Visual Studio 2013 SP2 或更高版本。
    2. 为您的数据获取 JSON 文档。如果它有可选字段,请确保您拥有的字段尽可能完整。
    3. 在 VS 的新 CS 文件中,选择编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类。

    这将创建一个对应于 JSON 数据的 C# 类。现在,您可以使用 JSON.NET 的 JsonConvert.DeserializeObject() 来创建它

    【讨论】:

      【解决方案5】:

      通过NewtonsoftJson库轻松解析数据

      例子

      dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata); //to parse data
      foreach (var product in x) { 
           Messagebox.show(product.data.ToString());
         }
      

      【讨论】:

        【解决方案6】:

        您可以将 Json 转换为 c# 类

        public class FaceRectangle  
        {
        
            public int top { get; set; }
            public int left { get; set; }
            public int width { get; set; }
            public int height { get; set; }
        }
        
        public class Emotion
        {
            public double anger { get; set; }
            public double contempt { get; set; }
            public double disgust { get; set; }
            public double fear { get; set; }
            public double happiness { get; set; }
            public double neutral { get; set; }
            public double sadness { get; set; }
            public double surprise { get; set; }
        }
        
        public class FaceAttributes
        {
            public double smile { get; set; }
            public string gender { get; set; }
            public double age { get; set; }
            public Emotion emotion { get; set; }
        }
        
        public class Example
        {
            public string faceId { get; set; }
            public FaceRectangle faceRectangle { get; set; }
            public FaceAttributes faceAttributes { get; set; }
        }
        
        Example Example_class = 
        Newtonsoft.Json.JsonConvert.DeserializeObject<Example>(json.ToString());
        

        请查看这个有用的链接 Json 到 C# 类 https://jsonutils.com/

        【讨论】:

          【解决方案7】:

          你的意思是这样的吗?反序列化为对象?

          using System.Web.Script.Serialization;
          
          public class FaceRectangle
          {
              public int top { get; set; }
              public int left { get; set; }
              public int width { get; set; }
              public int height { get; set; }
          }
          
          public class Emotion
          {
              public double anger { get; set; }
              public double contempt { get; set; }
              public double disgust { get; set; }
              public double fear { get; set; }
              public double happiness { get; set; }
              public double neutral { get; set; }
              public double sadness { get; set; }
              public double surprise { get; set; }
          }
          
          public class FaceAttributes
          {
              public double smile { get; set; }
              public string gender { get; set; }
              public double age { get; set; }
              public Emotion emotion { get; set; }
          }
          
          public class RootObject
          {
              public string faceId { get; set; }
              public FaceRectangle faceRectangle { get; set; }
              public FaceAttributes faceAttributes { get; set; }
          }
          
          return JsonConvert.DeserializeObject<RootObject>(jsonString);
          

          【讨论】:

          • 您的代码将返回 JSON 中的两个对象中的哪一个?
          【解决方案8】:

          我希望这会奏效

          string Jsondata = "Your Json data";

          public class Mainclass
          {
             public guid faceId;
             IEnumerable<faceRectangle>
             IEnumerable<faceAttributes>
          }
          public class faceRectangle
          {
          
          }
          
          public class faceAttributes
          {
          
          }
          
          
          Mainclass backdata =  JsonConvert.DeserializeObject<Mainclass>(Jsondata , new DataConverter())
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-04-28
            • 1970-01-01
            • 2011-02-02
            • 2021-06-02
            • 2015-06-07
            相关资源
            最近更新 更多