需要将 json 之后的(反序列化)转换为 C# 对象
Need to convert(Deserialize) following json into C# object
如何在 C# 对象中转换(反序列化)遵循 json 格式?
所有这些 json 文本一次性转换(反序列化),否则我需要一次又一次地转换子对象
我正在尝试这样的事情
var x = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json_data);
{
"Id":"1405de4d-2823-43b4-8dba-66c2714bc7f",
"Name":"Sports/Boxing",
"Parent":{
"Id":"88ada251-cff1-4eb7-bc47-2e6d366616a63",
"Name":"http://localhost:80/PDC/Sports/Boxing",
"DurationMilliseconds":227.2,
"StartMilliseconds":0,
"Children":[
{
"Id":"dbf36d18-8abd-43f1-ae9b-640cb3d77a87",
"Name":"xx",
"DurationMilliseconds":212,
"Milliseconds":15.1,
l
}
使用 http://jsonclassgenerator.codeplex.com/ 和手动调整,我将所需的 类 减少到 2(root 和 childs 类 实际上是相同的):
请注意,您可以为 类 取任何名称,link 和 JSON 是每个 属性 上方的 [JsonProperty("...")]
属性.
using System;
using Newtonsoft.Json;
namespace WpfApplication3
{
public partial class MainWindow
{
private readonly string json = @"
{
""Id"":""1405de4d-2823-43b4-8dba-66c2714bc7f"",
""Name"":""Sports/Boxing"",
""Started"":""\/Date(1472064057630)\/"",
""DurationMilliseconds"":227.2,
""MachineName"":""RED"",
""CustomLinks"":null,
""Root"":{
""Id"":""88ada251-cff1-4eb7-bc47-2e6d366616a63"",
""Name"":""http://localhost:80/PDC/Sports/Boxing"",
""DurationMilliseconds"":227.2,
""StartMilliseconds"":0,
""Children"":[
{
""Id"":""dbf36d18-8abd-43f1-ae9b-640cb3d77a87"",
""Name"":""Red Eagle"",
""DurationMilliseconds"":212,
""StartMilliseconds"":15.1,
""Children"":[
{
""Id"":""dbd7018d-421d-42bd-b0e5-fd3e9462cca0"",
""Name"":""Blue Eagle"",
""DurationMilliseconds"":106.8,
""StartMilliseconds"":120.4,
""Children"":[
{
""Id"":""c86199e0-d12b-4bd0-90ea-9f15a2618160"",
""Name"":""True Eagle"",
""DurationMilliseconds"":0.3,
""StartMilliseconds"":226.9,
""Children"":null,
""CustomTimings"":null
}
],
""CustomTimings"":null
}
],
""CustomTimings"":null
}
],
""CustomTimings"":null
},
""ClientTimings"":null,
""User"":""::1"",
""Storage"":null
}";
public MainWindow()
{
InitializeComponent();
var o = JsonConvert.DeserializeObject<MyObject>(json);
}
}
public class Child
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("DurationMilliseconds")]
public double DurationMilliseconds { get; set; }
[JsonProperty("StartMilliseconds")]
public double StartMilliseconds { get; set; }
[JsonProperty("Children")]
public Child[] Children { get; set; }
[JsonProperty("CustomTimings")]
public object CustomTimings { get; set; }
}
public class MyObject
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Started")]
public DateTime Started { get; set; }
[JsonProperty("DurationMilliseconds")]
public double DurationMilliseconds { get; set; }
[JsonProperty("MachineName")]
public string MachineName { get; set; }
[JsonProperty("CustomLinks")]
public object CustomLinks { get; set; }
[JsonProperty("Root")]
public Child Root { get; set; }
[JsonProperty("ClientTimings")]
public object ClientTimings { get; set; }
[JsonProperty("User")]
public string User { get; set; }
[JsonProperty("Storage")]
public object Storage { get; set; }
}
}
有两种方法:
您可以反序列化(使用 Newtonsoft.Json)到 JObject:
var jsonObj = JsonConvert.DeserializeObject(json) as JObject;
// Then you may access properties like this:
var id = jsonObj ["Id"].Value<int>();
此外,还可以遍历此 "tree" 对象。
更多详情请见 http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm
您可以从 JSON 生成 类(例如 http://json2csharp.com/)并编写如下内容:
var rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObjectType>(json);
如何在 C# 对象中转换(反序列化)遵循 json 格式?
所有这些 json 文本一次性转换(反序列化),否则我需要一次又一次地转换子对象
我正在尝试这样的事情
var x = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json_data);
{
"Id":"1405de4d-2823-43b4-8dba-66c2714bc7f",
"Name":"Sports/Boxing",
"Parent":{
"Id":"88ada251-cff1-4eb7-bc47-2e6d366616a63",
"Name":"http://localhost:80/PDC/Sports/Boxing",
"DurationMilliseconds":227.2,
"StartMilliseconds":0,
"Children":[
{
"Id":"dbf36d18-8abd-43f1-ae9b-640cb3d77a87",
"Name":"xx",
"DurationMilliseconds":212,
"Milliseconds":15.1,
l
}
使用 http://jsonclassgenerator.codeplex.com/ 和手动调整,我将所需的 类 减少到 2(root 和 childs 类 实际上是相同的):
请注意,您可以为 类 取任何名称,link 和 JSON 是每个 属性 上方的 [JsonProperty("...")]
属性.
using System;
using Newtonsoft.Json;
namespace WpfApplication3
{
public partial class MainWindow
{
private readonly string json = @"
{
""Id"":""1405de4d-2823-43b4-8dba-66c2714bc7f"",
""Name"":""Sports/Boxing"",
""Started"":""\/Date(1472064057630)\/"",
""DurationMilliseconds"":227.2,
""MachineName"":""RED"",
""CustomLinks"":null,
""Root"":{
""Id"":""88ada251-cff1-4eb7-bc47-2e6d366616a63"",
""Name"":""http://localhost:80/PDC/Sports/Boxing"",
""DurationMilliseconds"":227.2,
""StartMilliseconds"":0,
""Children"":[
{
""Id"":""dbf36d18-8abd-43f1-ae9b-640cb3d77a87"",
""Name"":""Red Eagle"",
""DurationMilliseconds"":212,
""StartMilliseconds"":15.1,
""Children"":[
{
""Id"":""dbd7018d-421d-42bd-b0e5-fd3e9462cca0"",
""Name"":""Blue Eagle"",
""DurationMilliseconds"":106.8,
""StartMilliseconds"":120.4,
""Children"":[
{
""Id"":""c86199e0-d12b-4bd0-90ea-9f15a2618160"",
""Name"":""True Eagle"",
""DurationMilliseconds"":0.3,
""StartMilliseconds"":226.9,
""Children"":null,
""CustomTimings"":null
}
],
""CustomTimings"":null
}
],
""CustomTimings"":null
}
],
""CustomTimings"":null
},
""ClientTimings"":null,
""User"":""::1"",
""Storage"":null
}";
public MainWindow()
{
InitializeComponent();
var o = JsonConvert.DeserializeObject<MyObject>(json);
}
}
public class Child
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("DurationMilliseconds")]
public double DurationMilliseconds { get; set; }
[JsonProperty("StartMilliseconds")]
public double StartMilliseconds { get; set; }
[JsonProperty("Children")]
public Child[] Children { get; set; }
[JsonProperty("CustomTimings")]
public object CustomTimings { get; set; }
}
public class MyObject
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Started")]
public DateTime Started { get; set; }
[JsonProperty("DurationMilliseconds")]
public double DurationMilliseconds { get; set; }
[JsonProperty("MachineName")]
public string MachineName { get; set; }
[JsonProperty("CustomLinks")]
public object CustomLinks { get; set; }
[JsonProperty("Root")]
public Child Root { get; set; }
[JsonProperty("ClientTimings")]
public object ClientTimings { get; set; }
[JsonProperty("User")]
public string User { get; set; }
[JsonProperty("Storage")]
public object Storage { get; set; }
}
}
有两种方法:
您可以反序列化(使用 Newtonsoft.Json)到 JObject:
var jsonObj = JsonConvert.DeserializeObject(json) as JObject; // Then you may access properties like this: var id = jsonObj ["Id"].Value<int>();
此外,还可以遍历此 "tree" 对象。 更多详情请见 http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm
您可以从 JSON 生成 类(例如 http://json2csharp.com/)并编写如下内容:
var rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObjectType>(json);