解析 json Azure API 管理订阅数据失败
Parse json Fails for Azure API Management subscription data
我运行post获取API管理订阅数据但是无法解析返回的json也找不到对返回字符串进一步过滤的内容是必需的。
邮递员显示 json 很好。其他 APIM REST 请求解析 json 就好了。
有人可以解释一下吗?
string url = $"https://management.azure.com/subscriptions/XXX/resourceGroups/YYY/providers/Microsoft.ApiManagement/service/ZZZ/subscriptions?api-version=2019-12-01&$filter=(contains(name, '{myname}'))";
string json = null;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken);
using (HttpResponseMessage response2 = client.GetAsync(url).Result)
{
using (HttpContent content = response2.Content)
{
json = content.ReadAsStringAsync().Result;
//JObject s = JObject.Parse(json);//didn't work
//Console.WriteLine((string)s["name"]);//didn't work
//var user = JsonConvert.DeserializeObject<UserAPIMSubscriptionMetaData>(json);//didn't work
//var user3 = JsonConvert.DeserializeObject(json);//didn't work
var json2 = json.Replace("\r\n", "");
json2 = json2.Replace(" ", "");// first replace didn't work so tried adding this one too but didn't work either
//var a = JObject.Parse(json2);//tried this too but didn't work
var user = JsonConvert.DeserializeObject<UserAPIMSubscriptionMetaData>(json2);
}
}
}
internal class properties
{
[JsonProperty("ownerId")]
public string ownerId { get; set; }
[JsonProperty("scope")]
public string scope { get; set; }
[JsonProperty("displayName")]
public string displayName { get; set; }
[JsonProperty("state")]
public string state { get; set; }
[JsonProperty("createdDate")]
public string createdDate { get; set; }
[JsonProperty("startDate")]
public string startDate { get; set; }
[JsonProperty("expirationDate")]
public string expirationDate { get; set; }
[JsonProperty("endDate")]
public string endDate { get; set; }
[JsonProperty("notificationDate")]
public string notificationDate { get; set; }
[JsonProperty("stateComment")]
public string stateComment { get; set; }
[JsonProperty("allowTracing")]
public string allowTracing { get; set; }
}
internal class UserAPIMSubscriptionMetaData
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("type")]
public string thisType { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("properties")]
public properties properties { get; set; }
}
ReadAsStringAsync() 的初始结果值:
"{\r\n \"value\": [\r\n {\r\n \"id\": \"/subscriptions/XXXXX/resourceGroups/ZZZZ/providers/Microsoft.ApiManagement/service/YYYYY/subscriptions/sergiosolorzanoSubscription\",\r\n \"type\": \"Microsoft.ApiManagement/service/subscriptions\",\r\n \"name\": \"sergiosolorzanoSubscription\",\r\n \"properties\": {\r\n \"ownerId\": \"/subscriptions/XXXX/resourceGroups/YYYY/providers/Microsoft.ApiManagement/service/ZZZ/users/sergiosolorzanogmailcom\",\r\n \"scope\": \"/subscriptions/XXXX/resourceGroups/JJJJJ/providers/Microsoft.ApiManagement/service/ZZZZ/apis\",\r\n \"displayName\": \"sergiosolorzanoSubscription\",\r\n \"state\": \"active\",\r\n \"createdDate\": \"2020-04-23T08:04:31.737Z\",\r\n \"startDate\": null,\r\n \"expirationDate\": null,\r\n \"endDate\": null,\r\n \"notificationDate\": null,\r\n \"stateComment\": null,\r\n \"allowTracing\": true\r\n }\r\n }\r\n ],\r\n \"count\": 1\r\n}"
我不知道你得到的确切错误,但我怀疑问题是你没有正确反序列化它。
如果您查看 JSON,您尝试解析的对象位于 value
中,这是一个 Json 数组。您现在要做的是将该数组解析为该数组包含的对象。
您需要做的是制作第三个对象作为其他对象的 "Container"
public class Container
{
[JsonProperty("value")]
public IEnumerable<UserAPIMSubscriptionMetaData> Metadata {get; set;}
[JsonProperty("count")]
public int Count {get; set;}
}
或者您可以将 Json 解析为一个 JObject 并标记为值,这样您的 JSON 就变成了元数据对象的数组,然后您可以将其反序列化为对象的集合。
var jobj = JObject.Parse(json);
var newjson = jobj["value"].ToString();
var myobj = JsonConvert.DeserializeObject<IEnumerable<UserAPIMSubscriptionMetaData>>(newjson);
备注:
我用IEnumerable<>
作为例子,你可以使用你想要的任何类型的集合。数组、列表等
此外,您应该使用正确的 Async/Await 模式而不是 .Result
,因为 .Result
会导致阻塞。有关 .Result
与 await
的信息,请参阅 Jon Skeet 在 this question 上的回答。
我运行post获取API管理订阅数据但是无法解析返回的json也找不到对返回字符串进一步过滤的内容是必需的。
邮递员显示 json 很好。其他 APIM REST 请求解析 json 就好了。
有人可以解释一下吗?
string url = $"https://management.azure.com/subscriptions/XXX/resourceGroups/YYY/providers/Microsoft.ApiManagement/service/ZZZ/subscriptions?api-version=2019-12-01&$filter=(contains(name, '{myname}'))";
string json = null;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken);
using (HttpResponseMessage response2 = client.GetAsync(url).Result)
{
using (HttpContent content = response2.Content)
{
json = content.ReadAsStringAsync().Result;
//JObject s = JObject.Parse(json);//didn't work
//Console.WriteLine((string)s["name"]);//didn't work
//var user = JsonConvert.DeserializeObject<UserAPIMSubscriptionMetaData>(json);//didn't work
//var user3 = JsonConvert.DeserializeObject(json);//didn't work
var json2 = json.Replace("\r\n", "");
json2 = json2.Replace(" ", "");// first replace didn't work so tried adding this one too but didn't work either
//var a = JObject.Parse(json2);//tried this too but didn't work
var user = JsonConvert.DeserializeObject<UserAPIMSubscriptionMetaData>(json2);
}
}
}
internal class properties
{
[JsonProperty("ownerId")]
public string ownerId { get; set; }
[JsonProperty("scope")]
public string scope { get; set; }
[JsonProperty("displayName")]
public string displayName { get; set; }
[JsonProperty("state")]
public string state { get; set; }
[JsonProperty("createdDate")]
public string createdDate { get; set; }
[JsonProperty("startDate")]
public string startDate { get; set; }
[JsonProperty("expirationDate")]
public string expirationDate { get; set; }
[JsonProperty("endDate")]
public string endDate { get; set; }
[JsonProperty("notificationDate")]
public string notificationDate { get; set; }
[JsonProperty("stateComment")]
public string stateComment { get; set; }
[JsonProperty("allowTracing")]
public string allowTracing { get; set; }
}
internal class UserAPIMSubscriptionMetaData
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("type")]
public string thisType { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("properties")]
public properties properties { get; set; }
}
ReadAsStringAsync() 的初始结果值:
"{\r\n \"value\": [\r\n {\r\n \"id\": \"/subscriptions/XXXXX/resourceGroups/ZZZZ/providers/Microsoft.ApiManagement/service/YYYYY/subscriptions/sergiosolorzanoSubscription\",\r\n \"type\": \"Microsoft.ApiManagement/service/subscriptions\",\r\n \"name\": \"sergiosolorzanoSubscription\",\r\n \"properties\": {\r\n \"ownerId\": \"/subscriptions/XXXX/resourceGroups/YYYY/providers/Microsoft.ApiManagement/service/ZZZ/users/sergiosolorzanogmailcom\",\r\n \"scope\": \"/subscriptions/XXXX/resourceGroups/JJJJJ/providers/Microsoft.ApiManagement/service/ZZZZ/apis\",\r\n \"displayName\": \"sergiosolorzanoSubscription\",\r\n \"state\": \"active\",\r\n \"createdDate\": \"2020-04-23T08:04:31.737Z\",\r\n \"startDate\": null,\r\n \"expirationDate\": null,\r\n \"endDate\": null,\r\n \"notificationDate\": null,\r\n \"stateComment\": null,\r\n \"allowTracing\": true\r\n }\r\n }\r\n ],\r\n \"count\": 1\r\n}"
我不知道你得到的确切错误,但我怀疑问题是你没有正确反序列化它。
如果您查看 JSON,您尝试解析的对象位于 value
中,这是一个 Json 数组。您现在要做的是将该数组解析为该数组包含的对象。
您需要做的是制作第三个对象作为其他对象的 "Container"
public class Container
{
[JsonProperty("value")]
public IEnumerable<UserAPIMSubscriptionMetaData> Metadata {get; set;}
[JsonProperty("count")]
public int Count {get; set;}
}
或者您可以将 Json 解析为一个 JObject 并标记为值,这样您的 JSON 就变成了元数据对象的数组,然后您可以将其反序列化为对象的集合。
var jobj = JObject.Parse(json);
var newjson = jobj["value"].ToString();
var myobj = JsonConvert.DeserializeObject<IEnumerable<UserAPIMSubscriptionMetaData>>(newjson);
备注:
我用IEnumerable<>
作为例子,你可以使用你想要的任何类型的集合。数组、列表等
此外,您应该使用正确的 Async/Await 模式而不是 .Result
,因为 .Result
会导致阻塞。有关 .Result
与 await
的信息,请参阅 Jon Skeet 在 this question 上的回答。