Xamarin JSON Deserialize
我创建了一个httpwebrequest来检查用户的用户名和密码是否正确。如果用户的用户名和密码正确,它将返回一个带有用户contactID的JSON数组。我试图反序列化JSON,但未能获得实际数据。我想获取联系人ID并将数据发送到下一页的变量。
用户名和密码正确时JSON的输出:
[{"ContactID":"1"}]
我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Net; using System.Text; using System.Windows.Input; using TBSMobileApplication.Data; using TBSMobileApplication.View; using Xamarin.Essentials; using Xamarin.Forms; namespace TBSMobileApplication.ViewModel { public class LoginPageViewModel : INotifyPropertyChanged { void OnProperyChanged(string PropertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); } public string username; public string password; public string Username { get { return username; } set { username = value; OnProperyChanged(nameof(Username)); } } public string Password { get { return password; } set { password = value; OnProperyChanged(nameof(Password)); } } public ICommand LoginCommand { get; set; } public LoginPageViewModel() { LoginCommand = new Command(OnLogin); } public void OnLogin() { if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) { MessagingCenter.Send(this,"Login Alert", Username); } else { var current = Connectivity.NetworkAccess; if (current == NetworkAccess.Internet) { var link ="http://192.168.1.25:7777/TBS/test.php?User=" + Username +"&Password=" + Password; var request = HttpWebRequest.Create(string.Format(@link)); request.ContentType ="application/json"; request.Method ="GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode); } else { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var content = reader.ReadToEnd(); if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content)) { MessagingCenter.Send(this,"Http", Username); } else { var usr = JsonConvert.DeserializeObject(content); App.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(), true); } } } } } else { MessagingCenter.Send(this,"Not Connected", Username); } } } public event PropertyChangedEventHandler PropertyChanged; } } |
修改代码
1 2 3 4 5 6 7 8 9 10 11 | if (content.Equals("[]") || string.IsNullOrWhiteSpace(content) || string.IsNullOrEmpty(content)) { MessagingCenter.Send(this,"Http", Username); } else { var response = JsonConvert.DeserializeObject<List<LoggedInUser>>(content); var contactId=response[0].ContactID; //response have your ContactID value. Try to debug & see. App.Current.MainPage.Navigation.PushAsync(new DatabaseSyncPage(), true); } |
创建另一个公共类来反序列化响应
1 2 3 4 | public class LoggedInUser { public string ContactID { get; set; } } |
如果结果中有超过1条记录(正如您在下面的评论中所问的那样)你可以通过循环得到它们
1 2 3 4 5 | for (int i = 0; i < response.Count; i++) { var item = response[i]; var contactId = item.ContactId; } |
希望对你有帮助。
JSON响应对象找不到输出结果的标准格式。不管JSON反序列化是什么,您应该根据下面的代码创建单独的类。
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class RootObject { public string ContactID { get; set; } } Public Void ServiceRequest() { var content = reader.ReadToEnd(); if(!String.IsNullOrEmpty(content) { var response = JsonConvert.DeserializeObject<RootObject>(content); } } |
我希望它会有用。