【发布时间】:2020-08-15 23:41:09
【问题描述】:
我希望在返回 JSON 时能够使用 LINQ 访问 JSON 对象。
我提到了Send JSON via POST in C# and Receive the JSON returned?和Send and receive json via HttpClient
这是我目前所拥有的
public static async Task<string> GetMailTip(string user)
{
var jsonData = new StringContent(FormatJson(CreateJsonGettingMailTip(user)), Encoding.UTF8, "application/json");
var payload = await client.PostAsync($"https://graph.microsoft.com/v1.0/users/{user}/getMailTips", jsonData);
string responseContent = "";
if (payload.Content != null)
{
responseContent = await payload.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
var getMailTip = responseContent["value"]
.Children()
.Where(i => i != null)
.Select(c => c[""][""].Value<string>().Trim());
return responseContent;
}
返回的 JSON 是
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.mailTips)",
"value": [
{
"emailAddress": {
"name": "",
"address": ""
},
"automaticReplies": {
"message": "",
"messageLanguage": {
"locale": "",
"displayName": ""
},
"scheduledStartTime": {
"dateTime": "",
"timeZone": ""
},
"scheduledEndTime": {
"dateTime": "",
"timeZone": ""
}
}
}
]
}
我希望能够使用 LINQ 访问 JSON 中的 message 属性
任何帮助将不胜感激
【问题讨论】:
-
不清楚你在问什么,或者你想做什么。
-
你需要将
responseContent反序列化为c#类对象。您可以使用jsonutils.com 为您拥有的 json 字符串创建 c# 类。 -
@MichaelRandall 请查看修改后的问题
-
这能回答你的问题吗? Linq query JObject
-
如果你想要一些快速的东西,那么没有比将 JSON 粘贴到 QuickType.io 之类的服务中更快的方法了,让它为你生成类和 json 反序列化代码,然后将其粘贴到你的项目中用一个衬里(他们也建议)将你的json变成类确实是要走的路。但是定义这样做比将Json粘贴到SO中并写一个Q更快;将这些东西作为适当的类与将查询转换为字符串词典等词典相比,编写查询会更容易、更快、更准确