Deserializing json data from webservice

时间:2018-12-03 13:01:43

标签: json xamarin.android deserialization json-deserialization android-webservice

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

0 个答案:

没有答案