反序列化json在xamarin形式中显示null

时间:2019-02-21 04:28:42

标签: json xamarin.forms

嗨,我有一个Login API,我正在通过我的xamarin.forms应用程序进行登录。 我将发布用户名和密码,作为回报,我得到了一些数据。现在,Iam在反序列化json时遇到了一些问题。我在我的resultJson上获取数据,但我无法反序列化它。救救我。

我的杰森(Json):

[
{
"Result":true,
"ID":"fc938df0",
"LoginName":"test",
"UserName":"test",
"ConnectionString":"MSSQLSERVER;Initial Catalog=Test1;User ID=db;Password=db@2018",
"UserProfileID":"fc938df0"
}
]

我的具有Json反序列化的API调用类。

    public T APICallResult<T>()
        {
            try
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    if (loadingIndicator != null)
                    {
                        loadingIndicator.IsRunning = true;
                        loadingIndicator.IsVisible = true;
                    }
                });
                var client = new HttpClient { BaseAddress = baseAddress };
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var req = new HttpRequestMessage(HttpMethod.Post, apiurl);
                req.Content = new StringContent(postdata, Encoding.UTF8, "application/json");
                string stringObtained = "";

                Task<string> task = Task.Run(async () => await Threading(client, req));
                task.Wait();
                stringObtained = task.Result;

                var jsonObtained = Regex.Unescape(stringObtained);
                int startIndex = jsonObtained.IndexOf('[');
                int endIndex = jsonObtained.LastIndexOf(']');
                int length = endIndex - startIndex + 1;
                var resultJSON = jsonObtained.Substring(startIndex, length);


                T resultObject;//Generic type object
                try
                {

//**Deserializing**
                    resultObject = JsonConvert.DeserializeObject<T>(resultJSON);//, settings);  
                    removeLoadingAnimation();
                    return resultObject;
                }
                catch (Exception e)
                {
                    List<ErrorMessageData> errorMessages = JsonConvert.DeserializeObject<List<ErrorMessageData>>(resultJSON);
                    errorMessage = errorMessages[0];
                    removeLoadingAnimation();
                    return default(T);
                }
            }
            catch (Exception e)
            {
                errorMessage = new ErrorMessageData();
                errorMessage.Flag = false;
                errorMessage.Message = e.Message;
                removeLoadingAnimation();
                return default(T);
            }
        }

登录类时我的API调用

string postdataForLogin = "{\"userName\":\"" + userName.Text + "\",\"password\":\"" + password.Text + "\",\"RequestURL\":\"" + CommonValues.RequestURL + "\"}";
        APICall callForLogin = new APICall("/API/LoginMobile/HomeLogin", postdataForLogin, loadingIndicator);           
        try
        {
            List<LoginData> resultObjForLogin = callForLogin.APICallResult <List<LoginData>>();       
            if (resultObjForLogin != null)
            {


                LoginData loginData = new LoginData();
                loginData = resultObjForLogin[0];
                Settings.userID = loginData.UserProfileID;
                Settings.connectionString = loginData.ConnectionString;
                if (loginData.Result)
                {
                    Device.BeginInvokeOnMainThread(async () =>
                    {                         
                        Navigation.InsertPageBefore(new MainPage(), this);
                        await Navigation.PopAsync();
                    });
                }

我的数据模型

  public class LoginData
    {

         public bool Result { get; set; }            
         public string ID { get; set; }
         public string UserProfileID { get; set; }
         public string LoginName { get; set; }
         public string UserName { get; set; }
         public string ConnectionString { get; set; }

    }

1 个答案:

答案 0 :(得分:0)

这似乎很奇怪,将我的xamarin.forms从最新版本降级到预发行版4.0.0.169046- pre5后,我的问题解决了。我认为它是一个xamarin形式的错误