简体   繁体   中英

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.

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. 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 . 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

public async Task 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.

And it arrives at this line:

return 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

[{"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.

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.

We have to create the model class manually, and use it to deserialize, I recommend you to use json2csharp .

JsonConvert.DeserializeObject<List<RootObject>>(content);

public class RootObject
{
    public string COLUMN_NAME1 { get; set; }
    public string COLUMN_NAME2 { get; set; }
    ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM