0

globally, I have the following object:

public class Geraet
{
    public long Geraetenr { get; set; }
    public int Typ { get; set; }
    public string Platz { get; set; }
    public string Bezeichnung { get; set; }
    public int Tr { get; set; }
    public string Ip { get; set; }
    public string Bespielt { get; set; }
}

I populate a list of those objects, serialize them and send them via webservice:

    [HttpGet]
    public IHttpActionResult Get_Feedback()
    {

        List<Geraet> geraeteliste = null;

        try
        {
            geraeteliste = GetSpielgeraeteFromDatabase();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
        }
        if (geraeteliste == null)
        {
            return Ok("No record found!");
        }
        else
        {
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(geraeteliste);
            return Json(json);
        }
    }

The data received by webservice looks like the following:

"[{\"Geraetenr\":123456789,\"Typ\":61,\"Platz\":\"1-01\",\"Bezeichnung\":\"CSII ADM430\",\"Tr\":3,\"Ip\":\"123.123.123.123\",\"Bespielt\":\"0\"},{\"Geraetenr\":987654321,\"Typ\":61,\"Platz\":\"2-12\",\"Bezeichnung\":\"M-BOX PUR+ GOLD\",\"Tr\":3,\"Ip\":\"124.124.124.124\",\"Bespielt\":\"0\"}]"

In my Xamarin App, I have the same object given above and trying to deserialize it:

    private List<Geraet> GetSpielgeraeteFromWebservice()
    {
        List<Geraet> geraeteliste;

        var request = HttpWebRequest.Create(Constants.GeraetelistServicePath);
        request.ContentType = "application/json";
        request.Method = "GET";

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                var json = reader.ReadToEnd();

                geraeteliste = JsonConvert.DeserializeObject<List<Geraet>>(json);             
            }
        }

        return geraeteliste;
    }  

Unfortunately, I get an runtime error in the line geraeteliste = JsonConvert.DeserializeObject<List<Geraet>>(json); saying:

Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Error converting value "[{"Geraetenr":123456789,"Typ":61,"Platz":"1-01","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"123.123.123.123","Bespielt":"0"},{"Geraetenr":987654321,"Typ":61,"Platz":"2-12","Bezeichnung":"M-BOX PUR+ GOLD","Tr":3,"Ip":"124.124.124.124","Bespielt":"0"}]" to type 'System.Collections.Generic.List`1[GroceryList.Classes.Geraet]'. Path '', line 1, position 3421.

The sending / retrieving stuff does work, otherwise error message would be in the line var json = reader.ReadToEnd(); or I wouldn't have gotten the right values in the error message. So something with the deserialization does not work. Can anyone maybe help and tell me what is or could be the problem? Why can't he convert? It is the right order and the right values?

Best regards

Sam
  • 145
  • 1
  • 1
  • 12
  • this should be work can you check what is returning in reader.ReadToEnd(); – Negi Rox Dec 03 '18 at 13:28
  • Can you tell me how to check this? I tried to assign the complete string to a TextView: FindViewById(Resource.Id.textView).Text = GetSpielgeraeteFromWebservice(){ string json; var request = HttpWebRequest.Create(Constants.GeraetelistServicePath); request.ContentType = "application/json"; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { json = reader.ReadToEnd();); } } return json.ToString(); }; – Sam Dec 03 '18 at 14:10
  • But now the error message tells me: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. – Sam Dec 03 '18 at 14:12
  • Initialize your list first. Put var abc = JsonConvert.DeserializeObject>(json); And then geraetelist = abc; – Wasif Mahmood Mustafa Dec 03 '18 at 15:22
  • In a method you should assign a default value to any variable – Negi Rox Dec 04 '18 at 05:26

0 Answers0