Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
[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:

C#
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

What I have tried:

Different object modifications
Posted
Comments
[no name] 3-Dec-18 9:18am    
suspicious: line 1, position 3421
Member 14075735 3-Dec-18 9:27am    
häh?^^ sorry didn't get that... what were you trying to tell me? xD What is meant by position 3421? :X
[no name] 3-Dec-18 9:30am    
The data received is about 247 characters Long and the error is on Position 3421...
Member 14075735 3-Dec-18 9:42am    
So is the received data too long? Is that the error?
[no name] 3-Dec-18 9:51am    
No, the received data is not too Long. You Need to use the Debugger to inspect the received data at Position 3421 (if the answer is really that long). According to the execption message your received data is 247 characters Long but the error is on Position 3421, which is a contradiction

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900