如何将 Json 转换为 C# 对象?
How to convert Json into C# object?
我有一个特定的 Json 想要转换为 C# class 对象:
Json:
{
"Database": "DEMO",
"Schema": "PUBLIC",
"Warehouse": "TEST_WH",
"Query": "Call proc();",
"Role": "SYSADMIN",
"IsOpsTestRequest": "false",
"QueryParameters": {
"Param1": "abc",
"Param2": "def"
}
}
Class:
public class Request_Body
{
public string Database { get; set; }
public string Schema { get; set; }
public string Warehouse { get; set; }
public string Query { get; set; }
public string Role { get; set; }
public string IsOpsTestRequest { get; set; }
public List<(string, string)> QueryParameters { get; set; }
}
我在向我的 azure 函数 http 函数(持久函数)发出 post 请求时发送此 json,然后我尝试将此 json 转换为 C# 对象。
如果我从 class 中删除 QueryParameter
列表变量并从 json 中删除 QueryParameter
比它运行完美,但我希望这个 QueryParameter
在 json 并且它应该是通用的 - 它可以包含更多属性,例如 param1
和 param2
或有时可能更多。所以它应该是通用的。
在我的 http 触发器函数中,我将 json 转换为 class 对象,如下所示:
Request_Body data = await req.Content.ReadAsAsync<Request_Body>();
如何反序列化 json?
QueryParameters
的 JSON 不代表列表或数组。 JSON 中的列表或数组在值周围有方括号 ([...]
)。
但是,JSON可以用Dictionary<string, string>
表示。所以你的 class 应该是这样的:
public class Request_Body
{
public string Database { get; set; }
public string Schema { get; set; }
public string Warehouse { get; set; }
public string Query { get; set; }
public string Role { get; set; }
public string IsOpsTestRequest { get; set; }
public Dictionary<string, string> QueryParameters { get; set; }
}
尝试 newtonsoft json 来自 nuget 的包并使用序列化和反序列化方法。例如:
public class Account {
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; } }
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
我有一个特定的 Json 想要转换为 C# class 对象:
Json:
{
"Database": "DEMO",
"Schema": "PUBLIC",
"Warehouse": "TEST_WH",
"Query": "Call proc();",
"Role": "SYSADMIN",
"IsOpsTestRequest": "false",
"QueryParameters": {
"Param1": "abc",
"Param2": "def"
}
}
Class:
public class Request_Body
{
public string Database { get; set; }
public string Schema { get; set; }
public string Warehouse { get; set; }
public string Query { get; set; }
public string Role { get; set; }
public string IsOpsTestRequest { get; set; }
public List<(string, string)> QueryParameters { get; set; }
}
我在向我的 azure 函数 http 函数(持久函数)发出 post 请求时发送此 json,然后我尝试将此 json 转换为 C# 对象。
如果我从 class 中删除 QueryParameter
列表变量并从 json 中删除 QueryParameter
比它运行完美,但我希望这个 QueryParameter
在 json 并且它应该是通用的 - 它可以包含更多属性,例如 param1
和 param2
或有时可能更多。所以它应该是通用的。
在我的 http 触发器函数中,我将 json 转换为 class 对象,如下所示:
Request_Body data = await req.Content.ReadAsAsync<Request_Body>();
如何反序列化 json?
QueryParameters
的 JSON 不代表列表或数组。 JSON 中的列表或数组在值周围有方括号 ([...]
)。
但是,JSON可以用Dictionary<string, string>
表示。所以你的 class 应该是这样的:
public class Request_Body
{
public string Database { get; set; }
public string Schema { get; set; }
public string Warehouse { get; set; }
public string Query { get; set; }
public string Role { get; set; }
public string IsOpsTestRequest { get; set; }
public Dictionary<string, string> QueryParameters { get; set; }
}
尝试 newtonsoft json 来自 nuget 的包并使用序列化和反序列化方法。例如:
public class Account {
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; } }
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);