請我需要幫助,我需要從JSON對象獲得下面的用戶名和用戶ID值我如何獲得在Xamarin形式從JSON對象值
{"data":{"UserID":"MS100000041","RoleID":5}}
這是我的代碼:
public class UserDetails
{
public string UserID { get; set; }
public int RoleID { get; set; }
}
public async Task Login(string url)
{
try
{
var uri = new Uri(url);
HttpClient myClient = new HttpClient();
var response = await myClient.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var Item = JsonConvert.DeserializeObject<UserDetails>(content);
string userid = Item.UserID;
int roleid= Item.RoleID;
}
else
{
Application.Current.Properties["response"] = response;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
但是這個值userid和roleid顯示爲空。請我如何得到這些值
感謝你的快速反應,其實我正在使用Visual Studio IDE中。 Item.data.RoleID和item.data.UserID中的數據用紅色下劃線,表示ide無法訪問值 – Josconcept
如上所述,您的UserDetails對象聲明也必須更改爲符合JSON數據,或者您可以使用'dynamic'變量並在運行時訪問JSON。 – hellowstone
似乎在xamarin中使用'dynamic'不存在。 – hellowstone