将JSON数据反序列化为C#
能否帮助我将JSON反序列化为C#?
我只是在上面吃我的牙齿.. =。=。
我发现很多方法如何不解决这个xD,
我不想与美国共享
我希望等待您的建议。
有用的链接:
http://jsonviewer.stack.hu/
http://json2csharp.com/
JSON看起来.. 将JSON数据反序列化为C#
[
{
"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
}
}
}
]
您的JSON文档代表array
,所以您必须deseria它丽泽到集合
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; }
}
U这样做有多快...... U解决这个问题有多快... -.- Cuz U得到午餐刹车,所以你用三明治检查一下stackoverflow在手和U使这个xd ... 谢谢伙伴。 前: 我尝试像一些阵列..,我替换一些字符只得到以faceRectangle {=> [等 东西的作品,但我得到麻烦那里-.-(无SENS) 所以IList解决问题;) thx再 –
你的意思是这样?要反序列化为一个对象?
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中的两个对象中的哪一个会返回您的代码? –
如果你想从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);
这将抛出一个异常=> https://dotnetfiddle.net/3e4Wzd –
可以的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/
而不是给你映射到你只是盲目地复制JSON C#类,我会建议你产生这样一个自己的类。
- 确保您运行的是Visual Studio 2013 SP2或更高版本。
- 为您的数据获取JSON文档。如果它有可选字段,请确保您拥有的字段尽可能全。
- 在VS中的新CS文件中,选择编辑 - >选择性粘贴 - >将JSON粘贴为类。
这将创建一个对应于JSON数据的C#类。现在,你可以使用JSON.NET的JsonConvert.DeserializeObject()来创建它
首先您要反序列化,所以你可以手动创建,所有你需要的类或最快,最简单的方法 - 您可以复制JSON字符串,你有,那么在Visual Studio
编辑 - >选择性粘贴 - >粘贴JSON作为类
现在你有结构deserializing.Then比如,你可以使用Newtonsoft.Json(你可以从NuGet下载)。有一个泛型方法JsonConvert.DeserializeObject<T>()
,它会为你做所有的反序列化工作。另外,如果您要在class
结构中使用您自己的属性名称,则可以使用[JsonProperty("Name")]
属性,该属性将在将属性序列化为JSON时改变属性的名称,反之亦然。
哇。 Thx,很有帮助;) 每天我都会更好;) 最好的问候 –
不客气! – SeM
通过使用NewtonsoftJson库轻松地分析数据
例
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata); //to parse data
foreach (var product in x) {
Messagebox.show(product.data.ToString());
}
我希望这将工作
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())
你到目前为止试过了什么?你想将哪些C#对象反序列化为?你还没有给我们太多的继续。 –
*我不想与U分享*恐怕它不会那样工作。你需要分享你的尝试,并告诉我们它有什么问题。然后我们将能够帮助你。你也应该去那些主题https://stackoverflow.com/help/asking – Guy
不明白你想要什么:))) – QuietNaN