Deserializing json data from webservice

TBMSamTBMSam ✭✭Member ✭✭
edited December 2018 in Xamarin.Android

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

Best Answer

Answers

  • Ahsan_SiddiqueAhsan_Siddique ✭✭✭✭ PKMember ✭✭✭✭
    edited December 2018

    @TBMSam said:
    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

    Hi,

    First of all initialize your list and then use them. Like shown blow.

    List<Geraet> geraeteliste = new List<Geraet>();

    I saw both places in web service and in consuming web service you did not initialize your list object. Therefore you are facing runtime error.

  • JiriMatejkaJiriMatejka ✭✭✭ CZMember ✭✭✭

    Have you pasted the whole exception messge? It is referring to line 1, position 3421., I would guess the JSON string is much longer than the one pasted. And on position 3421 there is something unexpected, maybe a non-string in Typ or Tr member?

  • TBMSamTBMSam ✭✭ Member ✭✭

    Thank you for your replies.

    @Ahsan_Siddique I now changed the line in List geraeteliste = new List(); but error message stays exactly the same :(

    @JiriMatejka No, as the message was extremely long, i shortened it a little bit. Here is the complete one:

    Unhandled Exception:

    Newtonsoft.Json.JsonSerializationException: Error converting value "[{"Geraetenr":301503721,"Typ":61,"Platz":"1-01","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301510681,"Typ":61,"Platz":"1-02","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301314390,"Typ":61,"Platz":"1-03","Bezeichnung":"LS ADM1500","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":299614347,"Typ":61,"Platz":"1-04","Bezeichnung":"M-BOX PUR RED+","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":239302462,"Typ":61,"Platz":"1-05","Bezeichnung":"NSTAR DL XI","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301503720,"Typ":61,"Platz":"1-06","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":299624167,"Typ":61,"Platz":"1-07","Bezeichnung":"M-BOX PUR+ GOLD","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":277514956,"Typ":61,"Platz":"1-08","Bezeichnung":"AC SLANT II","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":293111490,"Typ":61,"Platz":"1-09","Bezeichnung":"LStar_2 ADM515","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":299619324,"Typ":61,"Platz":"1-10","Bezeichnung":"M-BOX PUR+ PLA","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":293111480,"Typ":61,"Platz":"1-11","Bezeichnung":"LStar_2 ADM515","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301306613,"Typ":61,"Platz":"1-12","Bezeichnung":"LS ADM1500","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":277512955,"Typ":61,"Platz":"2-01","Bezeichnung":"AC SLANT II","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":277512957,"Typ":61,"Platz":"2-02","Bezeichnung":"AC SLANT II","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301511346,"Typ":61,"Platz":"2-03","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":299618685,"Typ":61,"Platz":"2-04","Bezeichnung":"M-BOX PUR+ PLA","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":277804090,"Typ":61,"Platz":"2-05","Bezeichnung":"NSuperstar2_638","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301511345,"Typ":61,"Platz":"2-06","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":2975141641,"Typ":61,"Platz":"2-07","Bezeichnung":"M.SEV MTG P. HD","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":2975141642,"Typ":61,"Platz":"2-08","Bezeichnung":"M.SEV MTG P. HD","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":277514955,"Typ":61,"Platz":"2-09","Bezeichnung":"AC SLANT II","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301306528,"Typ":61,"Platz":"2-10","Bezeichnung":"LS ADM1500","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":301503433,"Typ":61,"Platz":"2-11","Bezeichnung":"CSII ADM430","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"},{"Geraetenr":299624164,"Typ":61,"Platz":"2-12","Bezeichnung":"M-BOX PUR+ GOLD","Tr":3,"Ip":"ipplaceholder","Bespielt":"0"}]" to type 'System.Collections.Generic.List`1[GroceryList.Classes.Geraet]'. Path '', line 1, position 3421.

    I now replaced all IP-strings with placeholders, but despite that the message is the orginal one.

  • TBMSamTBMSam ✭✭ Member ✭✭

    Hello @Ahsan_Siddique,

    thanks again for your help.

    I now changed the line
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(geraeteliste);
    return Json(json);

    To
    return Json(geraeteliste);

    and added
    List geraeteliste = new List();
    above [HttpGet]

    THANK YOU SOOO MUCH!!!! THAT WORKED PERFECTLY! :)

Sign In or Register to comment.