[英]JsonConvert.DeserializeObject<DataTable> throws an exception
Good day to all, 祝大家有美好的一天
I am using a downgraded version of XCode Version 9.2 (9C40b), Visual Studio 2017 Pro 15.6.6 and Xamarin 11.9.1.24. 我正在使用XCode版本9.2(9C40b),Visual Studio 2017 Pro 15.6.6和Xamarin 11.9.1.24的降级版本。
The context is the following: The phone makes a REST call to a middle tier which query a database and return the DataTable serialized in JSON. 上下文如下:电话对中间层进行REST调用,该中间层查询数据库并返回以JSON序列化的DataTable。 The phone would Deserialize the content to a DataTable. 手机会将内容反序列化为数据表。
I have search for a way to work around this problem with no luck. 我正在寻找一种方法来解决此问题,但没有运气。 I have seen multiple similar post but none of the solutions worked for me. 我看到过多个类似的帖子,但是没有一种解决方案适合我。 I believe this to be a limitation of Xamarin regarding iOS as stated here . 我相信这是Xamarin关于iOS的作为规定的限制在这里 。 But I would like to know if there exist a way to work around it. 但是我想知道是否存在解决它的方法。 Here is my code that causes the exception to be raised: 这是导致引发异常的代码:
In the ExecuteQuery function I have this code 在ExecuteQuery函数中,我有以下代码
public async Task ExecuteQuery() ... 公共异步任务ExecuteQuery()...
try
{
var stringContent = new StringContent(JsonConvert.SerializeObject(_Items.query), Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, stringContent);
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<DataTable>(content);
}
}
catch (Exception exception) when (exception is System.Net.WebException ||
exception is HttpRequestException ||
exception is SocketException)
{
if (exception.InnerException is System.Net.WebException)
{
System.Net.WebException e = exception.InnerException as System.Net.WebException;
Console.WriteLine(e.Status);
throw e;
}
}
catch (Exception ex)
{
Debug.WriteLine(@" ERROR {0}", ex.Message);
}
The exception I get is the following: 我得到的异常是以下内容:
Constructor on type 'System.ComponentModel.ComponentConverter' not found. 找不到类型为'System.ComponentModel.ComponentConverter'的构造方法。
And it arrives at this line: 它到达了这一行:
return JsonConvert.DeserializeObject(content); 返回JsonConvert.DeserializeObject(content);
Keep in mind that the above code runs fine on the simulator. 请记住,以上代码在模拟器上运行良好。 Any help would be appreciated. 任何帮助,将不胜感激。
Thank you 谢谢
EDIT: The return value is used in the following section of my code: 编辑:在我的代码的以下部分中使用了返回值:
Task<DataTable> myTaskDataTable = Querymanager.ExecuteQuery();
DataTable myDataTable = await myTaskDataTable;
if (myDataTable != null)
{
vListBL = new ObservableCollection<MyItem>();
foreach (DataRow row in myDataTable.Rows)
{
MyItem vItem = new MyItem();
vItem.value1 = row.ItemArray[0].ToString();
vItem.value2 = row.ItemArray[2].ToString();
vListBL.Add(vItem);
}
}
The content variable contains an array of the form content变量包含以下形式的数组
[{"COLUMN_NAME1":"value1","COLUMN_NAME2":"value2",...},{...},{...}]
which is the content of the database table I am querying. 这就是我要查询的数据库表的内容。 Each element in the array represent a row. 数组中的每个元素代表一行。 In each element of the array I have the columnX colon valueY separated by comma. 在数组的每个元素中,我都有以逗号分隔的columnX冒号valueY。
Due to the Limitations , the iPhone's kernel prevents an application from generating code dynamically Mono on the iPhone does not support any form of dynamic code generation. 由于这些限制 ,iPhone的内核阻止了应用程序动态生成代码。iPhone上的Mono不支持任何形式的动态代码生成。
We have to create the model class manually, and use it to deserialize, I recommend you to use json2csharp . 我们必须手动创建模型类,并将其用于反序列化,建议您使用json2csharp 。
JsonConvert.DeserializeObject<List<RootObject>>(content);
public class RootObject
{
public string COLUMN_NAME1 { get; set; }
public string COLUMN_NAME2 { get; set; }
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.