将JSON数据反序列化为C#

时间:2017-06-12 10:34:59

标签: c# json deserialization

你能帮我把JSON反序列化为C#吗?我只是吃了牙。=。=。

我发现很多方法如何解决这个xD,
我不想和你分享那个 我想等你的建议。

有用的链接:
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
         }
      }
   }
]

8 个答案:

答案 0 :(得分:1)

首先你需要要反序列化的类,这样你就可以手动创建或者最快最简单的方法 - 你可以复制你拥有的json字符串,然后在visual studio中

  

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

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

此外,如果要在class结构中使用自己的属性名称,可以使用[JsonProperty("Name")]属性,这将在序列化为JSON时更改属性的名称,反之亦然。 / p>

答案 1 :(得分:0)

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

return \Response::json([
'message' => 'Post Created Succesfully',
'data' => [$listing, $listing->garage] 
                      //Access collection of garage relation ship
]);

答案 2 :(得分:0)

如果你想要那个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);

答案 3 :(得分:0)

您可以将Json转换为c#Class

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#Class https://jsonutils.com/

答案 4 :(得分:0)

您的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

答案 5 :(得分:0)

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

  1. 确保您正在运行Visual Studio 2013 SP2或更高版本。
  2. 获取数据的JSON文档。如果它有可选字段,请确保您拥有的字段尽可能完整。
  3. 在VS中的新CS文件中,选择编辑 - &gt;选择性粘贴 - &gt;将JSON粘贴为类。
  4. 这将创建一个与JSON数据对应的C#类。现在,您可以使用JSON.NET的JsonConvert.DeserializeObject()来创建它

答案 6 :(得分:0)

使用NewtonsoftJson库轻松解析数据

实施例

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

答案 7 :(得分:0)

我希望这会起作用

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())