Json DeserializeObject仅由iOS引发异常

时间:2016-05-31 13:02:32

标签: xamarin xamarin.ios xamarin.forms

Android没有这个问题,但是当我在iOS中运行相同的工作代码时,我得到了异常

public async Task Login(string inlogData, string password, string gcm) 
    {
        Login login = new Dharma.Login ();
        login.inlog_data = inlogData;
        login.password = password;
        login.phone_token = gcm;

        HttpClient client = new HttpClient ();
        HttpResponseMessage response = null;

        string json = JsonConvert.SerializeObject(login, Formatting.Indented);
        client.Timeout = TimeSpan.FromMilliseconds (10000);
        client.BaseAddress = new Uri (ConstantVariabels.GetLoginUrl());
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var content = new StringContent (json, Encoding.UTF8, "application/json");

        try 
        {
            response = await client.PostAsync (client.BaseAddress, content);
            response.EnsureSuccessStatusCode ();
            var JsonResult = response.Content.ReadAsStringAsync ().Result;

            dynamic dynJson = JsonConvert.DeserializeObject(JsonResult);
            string token = dynJson ["api_token"];   //THIS LINE THROWS THE EXCEPTION
            LoginPageViewModel.LoginSucceeded(token, true);
        }
        catch(TaskCanceledException te) 
        {
            LoginPageViewModel.LoginSucceeded(te.Message.ToString(), false);
        }
        catch(HttpRequestException hre) 
        {
            LoginPageViewModel.LoginSucceeded(hre.Message.ToString(), false);
        }
        catch(Exception e)
        {
            LoginPageViewModel.LoginSucceeded("fail", false);
        }
    }

异常

消息:ref类型的解释器

堆栈跟踪:

 at Microsoft.Scripting.Interpreter.LightCompiler.CompileMethodCallExpression (System.Linq.Expressions.Expression expr) [0x00089] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1283 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x001ba] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1642 
  at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileConditionalExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00007] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:818 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x001d2] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1644 
  at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
  at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00012] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1593 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileBlockExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00028] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:379 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x003d0] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1683 
  at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileGotoExpression (System.Linq.Expressions.Expression expr) [0x0001f] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:955 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x0042d] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1689 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileAsVoid (System.Linq.Expressions.Expression expr) [0x00085] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1620 
  at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00006] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1591 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileConditionalExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00071] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:828 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x001d2] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1644 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileAsVoid (System.Linq.Expressions.Expression expr) [0x00085] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1620 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileBlockStart (System.Linq.Expressions.BlockExpression node) [0x000b0] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:405 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileBlockExpression (System.Linq.Expressions.Expression expr, Boolean asVoid) [0x00007] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:376 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileNoLabelPush (System.Linq.Expressions.Expression expr) [0x003d0] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1683 
  at Microsoft.Scripting.Interpreter.LightCompiler.Compile (System.Linq.Expressions.Expression expr) [0x00008] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:1729 
  at Microsoft.Scripting.Interpreter.LightCompiler.CompileTop (System.Linq.Expressions.LambdaExpression node) [0x00055] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/LightCompiler.cs:221 
  at Microsoft.Scripting.Generation.CompilerHelpers.LightCompile (System.Linq.Expressions.LambdaExpression lambda) [0x00000] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Generation/CompilerHelpers.cs:619 
  at System.Linq.Expressions.Compiler.LambdaCompiler.Compile (System.Linq.Expressions.LambdaExpression lambda, System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator) [0x00000] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System.Core/System.Linq.Expressions.Interpret/LambdaCompiler.cs:41 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile () [0x00000] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/LambdaExpression.cs:188 
  at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T] (System.Runtime.CompilerServices.CallSite`1 site, System.Object[] args) [0x00059] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/CallSiteBinder.cs:148 
  at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0) [0x0010d] in /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/UpdateDelegates.Generated.cs:255 
  at Dharma.HttpRequests+<Login>c__async4.MoveNext () [0x0019e] in /Users/laurenspouders/dharmapp/Dharma/Library/HttpRequests.cs:345 

3 个答案:

答案 0 :(得分:5)

我遇到了同样的问题,发现之前使用的Newtonsoft JSON用于引用&#34;值&#34;默认情况下,但现在还没有,所以只需添加.Value

string token = dynJson ["api_token"].Value;

答案 1 :(得分:0)

问题是dynJson ["api_token"]返回类型Newtonsoft.Json.Linq.JTokenType.String,您将其分配给string。因此隐式转换不会发生并给出异常。添加一个可行的显式转换。

string token = (string)dynJson ["api_token"];

你必须在Android中得到一些关于隐式演员的错误。

答案 2 :(得分:0)

我解决了我的问题。

public async Task Login(string inlogData, string password, string gcm) 
    {
        Login login = new Dharma.Login ();
        login.inlog_data = inlogData;
        login.password = password;
        login.phone_token = gcm;

        HttpClient client = new HttpClient ();
        HttpResponseMessage response = null;

        string json = JsonConvert.SerializeObject(login, Formatting.Indented);
        client.Timeout = TimeSpan.FromMilliseconds (10000);
        client.BaseAddress = new Uri (ConstantVariabels.GetLoginUrl());
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var content = new StringContent (json, Encoding.UTF8, "application/json");

        try 
        {
            response = await client.PostAsync (client.BaseAddress, content);
            response.EnsureSuccessStatusCode ();
            var JsonResult = response.Content.ReadAsStringAsync ().Result;

            dynamic dynJson = JsonConvert.DeserializeObject(JsonResult);

            string token = "";

            foreach (var obj in dynJson)
            {
                if(obj.Name.Equals("api_token")) 
                {
                     token = obj.Value.ToString();
                }
            }

            LoginPageViewModel.LoginSucceeded(token, true);
        }
        catch(TaskCanceledException te) 
        {
            LoginPageViewModel.LoginSucceeded(te.Message.ToString(), false);
        }
        catch(HttpRequestException hre) 
        {
            LoginPageViewModel.LoginSucceeded(hre.Message.ToString(), false);
        }
        catch(Exception e)
        {
            LoginPageViewModel.LoginSucceeded("fail", false);
        }
    }