Xamarin - 无法从源类型转换为目标类型

时间:2015-07-06 12:07:09

标签: c# android json xamarin json.net

我想在动态变量中反序列化一个json文档,但它会抛出

错误:

byte[] decodedString = Base64.decode(byte_arr, Base64.DEFAULT);

代码:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Cannot cast from source type to destination type.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00078>
[MonoDroid] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0006b>
[MonoDroid] at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
[MonoDroid] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
[MonoDroid] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/1879/5f55a9ef/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Java.Lang.IRunnable.cs:71
[MonoDroid] at (wrapper dynamic-method) object.461b928a-1fde-4250-8fe8-4ab69b1f0acd (intptr,intptr) <IL 0x00011, 0x0003b>
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
[AndroidRuntime] Shutting down VM   

Json看起来像这样:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Json; using System.Net; using System.IO; using System.Threading.Tasks; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Newtonsoft.Json; namespace HomecheckApp { [Activity(Label = "LoginActivity", MainLauncher = true)] public class LoginActivity : Activity { private const string apiKey = "*********************************************"; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Login); EditText email = FindViewById<EditText>(Resource.Id.editEmail); EditText password = FindViewById<EditText>(Resource.Id.editPassword); Button loginButton = FindViewById<Button>(Resource.Id.loginButton); // string url = "http://homecheck.192.168.1.102.xip.io/appapi/finduser?email" + email.Text + "&pass=" + password.Text + "&key=" + apiKey; loginButton.Click += async (sender, e) => { string url = "http://homecheck.********.eu/appapi/finduser?email=" + email.Text + "&pass=" + password.Text + "&key=" + apiKey; JsonValue json = await FetchUserAsync(url, apiKey); //This is where it breaks dynamic userInfo = JsonConvert.DeserializeObject(json); //********* Console.WriteLine("Email is: " + userInfo.email); }; } private async Task<JsonValue> FetchUserAsync(string url, string apiKey) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); request.ContentType = "application/json"; request.Method = "POST"; using(WebResponse response = await request.GetResponseAsync()) { using(Stream stream = response.GetResponseStream()) { JsonValue jsonResponse = await Task.Run(() => JsonObject.Load(stream)); Console.WriteLine(jsonResponse.ToString()); return jsonResponse; } } } } }

当我尝试使用{"error":false,"id":3,"name":"***********","surname":"*******","email":"***********@gmail.com","gsm":"1239102312930","company":"","address":3} 反序列化时出现相同的错误。这是IDE本身的问题(我使用的是Xamarin Studio)还是其他的东西?

1 个答案:

答案 0 :(得分:1)

JsonConvert.DeserializeObject()string作为其第一个参数。但是,您提供的类型为JsonValue的对象。我不太确定为什么编译器甚至允许这样做,但这很可能是你问题的根源。

简单地改变

dynamic userInfo = JsonConvert.DeserializeObject(json);

dynamic userInfo = JsonConvert.DeserializeObject(json.ToString());

至少应该修复你的异常。然而,这可能不是最优雅的解决方案。