C# 指定的强制转换在HttpClient请求中无效
我正在使用xamarin.forms开发一个应用程序,当执行GET请求以在视图中显示CompanyName和CompanyLogo时,它会引发以下异常:异常: System.InvalidCastException:指定的强制转换无效。发生。任何关于如何解决此问题的帮助都将不胜感激。谢谢下面是我的代码 我的Json响应:C# 指定的强制转换在HttpClient请求中无效,c#,json,httpclient,C#,Json,Httpclient,我正在使用xamarin.forms开发一个应用程序,当执行GET请求以在视图中显示CompanyName和CompanyLogo时,它会引发以下异常:异常: System.InvalidCastException:指定的强制转换无效。发生。任何关于如何解决此问题的帮助都将不胜感激。谢谢下面是我的代码 我的Json响应: { "Credentials": { "Token": "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408y
{
"Credentials": {
"Token": "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408yadbE+Qj6IuTnZ++C9q4Ty1W2f1quNYMKZxFBNZg==",
"Authenticated": true,
"SecretKey": null
},
"Companies": [
{
"CustomerID": 2,
"CompanyName": "Posworx",
"CompanyLogo": "\\admin.loyaltyworx.co.za\\Images\\LOYALTY-LW.png",
"Stores": [
{
"StoreID": 2,
"StoreNumber": null,
"StoreName": "Pos Store",
"StoreAddress": "218 Stamford Hill Road",
"StoreCity": "Durban",
"StoreRegion": "KZN",
"StoreCountry": "South Africa"
},
{
"StoreID": 4,
"StoreNumber": null,
"StoreName": "Pos Store",
"StoreAddress": "218 Mathews Meyiwa Road",
"StoreCity": "Durban",
"StoreRegion": "KZN",
"StoreCountry": "South Africa"
},
{
"StoreID": 3021,
"StoreNumber": null,
"StoreName": "Tes tStore",
"StoreAddress": "",
"StoreCity": "",
"StoreRegion": "",
"StoreCountry": ""
},
{
"StoreID": 3061,
"StoreNumber": null,
"StoreName": "tEST",
"StoreAddress": "",
"StoreCity": "Durban",
"StoreRegion": "",
"StoreCountry": ""
}
]
}
],
"IsError": false,
"ErrorMessage": null
}
public class Store
{
public class Credentials
{
public string Token { get; set; }
public bool Authenticated { get; set; }
public object SecretKey { get; set; }
}
public class StoreDetails
{
public int StoreID { get; set; }
public object StoreNumber { get; set; }
public string StoreName { get; set; }
public string StoreAddress { get; set; }
public string StoreCity { get; set; }
public string StoreRegion { get; set; }
public string StoreCountry { get; set; }
}
public class Company
{
public int CustomerID { get; set; }
public string CompanyName { get; set; }
public string CompanyLogo { get; set; }
public IList<Store> Stores { get; set; }
}
public class Stores
{
public Credentials Credentials { get; set; }
public IList<Company> Companies { get; set; }
public bool IsError { get; set; }
public object ErrorMessage { get; set; }
}
}
public async void LoadData()
{
Store.Stores store = new Store.Stores();
try
{
var content = "";
HttpClient client = new HttpClient();
var RestUrl = "/api/Company/GetCustomerCompanies";
client.BaseAddress = new Uri(RestUrl);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Giftworx-App", "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408yadbE+Qj6IuTnZ++C9q4Ty1W2f1quNYMKZxFBNZg==");
HttpResponseMessage response = await client.GetAsync(RestUrl);
content = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(content);
JObject objResponse = (JObject)jsonResponse["Companies"];
Dictionary<string, JArray> Items = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, JArray>>(objResponse.ToString());
MainListView.ItemsSource = Items.ToList();
}
catch(Exception ex)
{
string exception = ex.Message;
}
}
类别:
{
"Credentials": {
"Token": "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408yadbE+Qj6IuTnZ++C9q4Ty1W2f1quNYMKZxFBNZg==",
"Authenticated": true,
"SecretKey": null
},
"Companies": [
{
"CustomerID": 2,
"CompanyName": "Posworx",
"CompanyLogo": "\\admin.loyaltyworx.co.za\\Images\\LOYALTY-LW.png",
"Stores": [
{
"StoreID": 2,
"StoreNumber": null,
"StoreName": "Pos Store",
"StoreAddress": "218 Stamford Hill Road",
"StoreCity": "Durban",
"StoreRegion": "KZN",
"StoreCountry": "South Africa"
},
{
"StoreID": 4,
"StoreNumber": null,
"StoreName": "Pos Store",
"StoreAddress": "218 Mathews Meyiwa Road",
"StoreCity": "Durban",
"StoreRegion": "KZN",
"StoreCountry": "South Africa"
},
{
"StoreID": 3021,
"StoreNumber": null,
"StoreName": "Tes tStore",
"StoreAddress": "",
"StoreCity": "",
"StoreRegion": "",
"StoreCountry": ""
},
{
"StoreID": 3061,
"StoreNumber": null,
"StoreName": "tEST",
"StoreAddress": "",
"StoreCity": "Durban",
"StoreRegion": "",
"StoreCountry": ""
}
]
}
],
"IsError": false,
"ErrorMessage": null
}
public class Store
{
public class Credentials
{
public string Token { get; set; }
public bool Authenticated { get; set; }
public object SecretKey { get; set; }
}
public class StoreDetails
{
public int StoreID { get; set; }
public object StoreNumber { get; set; }
public string StoreName { get; set; }
public string StoreAddress { get; set; }
public string StoreCity { get; set; }
public string StoreRegion { get; set; }
public string StoreCountry { get; set; }
}
public class Company
{
public int CustomerID { get; set; }
public string CompanyName { get; set; }
public string CompanyLogo { get; set; }
public IList<Store> Stores { get; set; }
}
public class Stores
{
public Credentials Credentials { get; set; }
public IList<Company> Companies { get; set; }
public bool IsError { get; set; }
public object ErrorMessage { get; set; }
}
}
public async void LoadData()
{
Store.Stores store = new Store.Stores();
try
{
var content = "";
HttpClient client = new HttpClient();
var RestUrl = "/api/Company/GetCustomerCompanies";
client.BaseAddress = new Uri(RestUrl);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Giftworx-App", "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408yadbE+Qj6IuTnZ++C9q4Ty1W2f1quNYMKZxFBNZg==");
HttpResponseMessage response = await client.GetAsync(RestUrl);
content = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(content);
JObject objResponse = (JObject)jsonResponse["Companies"];
Dictionary<string, JArray> Items = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, JArray>>(objResponse.ToString());
MainListView.ItemsSource = Items.ToList();
}
catch(Exception ex)
{
string exception = ex.Message;
}
}
公共类存储
{
公共类凭据
{
公共字符串标记{get;set;}
公共布尔认证{get;set;}
公共对象SecretKey{get;set;}
}
公共类存储详细信息
{
public int StoreID{get;set;}
公共对象StoreNumber{get;set;}
公共字符串StoreName{get;set;}
公共字符串存储地址{get;set;}
公共字符串存储城市{get;set;}
公共字符串存储区域{get;set;}
公共字符串StoreCountry{get;set;}
}
公营公司
{
public int CustomerID{get;set;}
公共字符串CompanyName{get;set;}
公共字符串CompanyLogo{get;set;}
公共IList存储{get;set;}
}
公共类商店
{
公共凭据凭据{get;set;}
公共IList公司{get;set;}
公共布尔IsError{get;set;}
公共对象错误消息{get;set;}
}
}
请求:
{
"Credentials": {
"Token": "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408yadbE+Qj6IuTnZ++C9q4Ty1W2f1quNYMKZxFBNZg==",
"Authenticated": true,
"SecretKey": null
},
"Companies": [
{
"CustomerID": 2,
"CompanyName": "Posworx",
"CompanyLogo": "\\admin.loyaltyworx.co.za\\Images\\LOYALTY-LW.png",
"Stores": [
{
"StoreID": 2,
"StoreNumber": null,
"StoreName": "Pos Store",
"StoreAddress": "218 Stamford Hill Road",
"StoreCity": "Durban",
"StoreRegion": "KZN",
"StoreCountry": "South Africa"
},
{
"StoreID": 4,
"StoreNumber": null,
"StoreName": "Pos Store",
"StoreAddress": "218 Mathews Meyiwa Road",
"StoreCity": "Durban",
"StoreRegion": "KZN",
"StoreCountry": "South Africa"
},
{
"StoreID": 3021,
"StoreNumber": null,
"StoreName": "Tes tStore",
"StoreAddress": "",
"StoreCity": "",
"StoreRegion": "",
"StoreCountry": ""
},
{
"StoreID": 3061,
"StoreNumber": null,
"StoreName": "tEST",
"StoreAddress": "",
"StoreCity": "Durban",
"StoreRegion": "",
"StoreCountry": ""
}
]
}
],
"IsError": false,
"ErrorMessage": null
}
public class Store
{
public class Credentials
{
public string Token { get; set; }
public bool Authenticated { get; set; }
public object SecretKey { get; set; }
}
public class StoreDetails
{
public int StoreID { get; set; }
public object StoreNumber { get; set; }
public string StoreName { get; set; }
public string StoreAddress { get; set; }
public string StoreCity { get; set; }
public string StoreRegion { get; set; }
public string StoreCountry { get; set; }
}
public class Company
{
public int CustomerID { get; set; }
public string CompanyName { get; set; }
public string CompanyLogo { get; set; }
public IList<Store> Stores { get; set; }
}
public class Stores
{
public Credentials Credentials { get; set; }
public IList<Company> Companies { get; set; }
public bool IsError { get; set; }
public object ErrorMessage { get; set; }
}
}
public async void LoadData()
{
Store.Stores store = new Store.Stores();
try
{
var content = "";
HttpClient client = new HttpClient();
var RestUrl = "/api/Company/GetCustomerCompanies";
client.BaseAddress = new Uri(RestUrl);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-Giftworx-App", "K6Zi8VXfqWuthxgn3YEfrU6Bj/EKM7BqvSZcatFgvMx408yadbE+Qj6IuTnZ++C9q4Ty1W2f1quNYMKZxFBNZg==");
HttpResponseMessage response = await client.GetAsync(RestUrl);
content = await response.Content.ReadAsStringAsync();
JObject jsonResponse = JObject.Parse(content);
JObject objResponse = (JObject)jsonResponse["Companies"];
Dictionary<string, JArray> Items = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, JArray>>(objResponse.ToString());
MainListView.ItemsSource = Items.ToList();
}
catch(Exception ex)
{
string exception = ex.Message;
}
}
public异步void LoadData()
{
Store.Stores Store=新的Store.Stores();
尝试
{
var-content=“”;
HttpClient=新的HttpClient();
var RestUrl=“/api/Company/GetCustomerCompanies”;
client.BaseAddress=新Uri(RestUrl);
client.DefaultRequestHeaders.Accept.Add(新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“应用程序/json”);
client.DefaultRequestHeaders.Add(“X-Giftworx-App”、“K6Zi8VXfqWuthxgn3YEfrU6Bj/ekm7bqvszcatfgmx408yadbe+Qj6IuTnZ++c9q4ty1w2f1 qynmkzxfbnzg=”;
HttpResponseMessage response=wait client.GetAsync(RestUrl);
content=wait response.content.ReadAsStringAsync();
JObject jsonResponse=JObject.Parse(内容);
JObject objResponse=(JObject)jsonResponse[“公司”];
Dictionary Items=Newtonsoft.Json.JsonConvert.DeserializeObject(objResponse.ToString());
MainListView.ItemsSource=Items.ToList();
}
捕获(例外情况除外)
{
字符串异常=例如消息;
}
}
您试图反序列化到这一行的字典中:
Dictionary<string, JArray> Items = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, JArray>>(objResponse.ToString());
Dictionary Items=Newtonsoft.Json.JsonConvert.DeserializeObject(objResponse.ToString());
尝试使用您创建的类:
var Items = Newtonsoft.Json.JsonConvert.DeserializeObject<Stores>(content);
var Items=Newtonsoft.Json.JsonConvert.DeserializeObject(内容);
基于JSON对类进行建模可能会有问题。确保模型类与JSON完全一致,并检查相应的数据类型
下面是一个基于JSON字符串创建C#类的链接:
异步方法不应返回void。您不能等待它们,因此无法处理异常。另外,为什么不直接反序列化到
存储
?应该被反序列化对象更正。谢谢。此外,在VisualStudio中,您只需将JSON复制到剪贴板,然后进入“编辑>粘贴特殊内容>将JSON粘贴为类”