I am trying to use Newtonsoft.Json to serialize an object for http post request and after that to deserialize the response into a list of objects. The Newtonsoft.Json SerializeObject and DeserializeObject are not working and returning null (variables json and items in code below). They do not throw any exception. The code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TestXamarinApp
{
static class WebService
{
public static async Task GetObjave()
{
try
{
ObjavaPostItem objavapostitem = new ObjavaPostItem();
objavapostitem.device_id = "test_id";
objavapostit********em.lastObjava = "test value";
string json = JsonConvert.SerializeObject(objavapostitem);
string responseContent = "[{\"device_id\":\"test1\", \"lastObjava\":\"2018-01-28\"}, {\"device_id\":\"test2\", \"lastObjava\":\"2018-01-29\"}]";
List<ObjavaPostItem> items = JsonConvert.DeserializeObject<List<ObjavaPostItem>>(responseContent);
}
catch (Exception e)
{
string error = e.ToString();
}
}
}
public class ObjavaPostItem
{
public string device_id { get; set; }
public string lastObjava { get; set; }
}
}
The same code works perfectly in an WPF Application so I know that the problem is with Xamarin~~~~.
The app is a Xamarin, Cross Platform App, Blank App, Xamarin Forms, Portable Class Library (PCL).
I am debuging with Xamarin Live Player on LG Nexus 5x, Android 7.1.2
My environment is as follows:
Newtonsoft.Json version is 9.0.1 (the same problem with version 10.0.3)
Xamarin.Forms version is v2.5.0.122203
Visual Studio Community 2015 Version 14.0.25431.01 Update 3
Xamarin 4.8.0.760 (fc93f3f5b)
Xamarin Designer 4.8.192 (4c64462bc)
Xamarin.Android 8.1.0.13 (f241f51)
Xamarin.Android SDK 8.1.5.0 (HEAD/75f8c6838)
Xamarin.Android Reference Assemblies and MSBuild support.
Xamarin.iOS 11.6.1.4 (db807ec)
Xamarin.iOS and Xamarin.Mac SDK 11.6.1.4 (db807ec)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.