when sending values in json to webservice -asmx-, how to deserialize the List<object> receivend in asmx ?

Hello expert,

i'm sending a form serialized in json to a webservice.

here's the json send:
{"list":[{"name":"5_28_139","value":"5_28_139(46.19731236038988, 6.094872275390571)"},{"name":"8_28_142","value":"8_28_142(46.18306210568767, 6.099002148437535)"},{"name":"14_28_116","value":"14_28_116(46.20802512160607, 6.104505312500123)"},{"name":"35_28_117","value":"35_28_117(46.179991902568105, 6.139877556152214)"}]}


here's the way i recover the jason from my webmethod, in a List<object>:

  [WebMethod(EnableSession = true)] // todo:: ajouter : Description="do smthg"
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public string SetLorriesNewPosition(List<object> list)
  {

hte prob is that i dont know how to deserialize my object in order to use them in webservice, i've been trying different things like :

  JavaScriptSerializer jsSrlzer = new JavaScriptSerializer();
            ListItemCollection collection = jsSrlzer.Deserialize<ListItemCollection>(list.ToString());

but i dont succeed to recover object which will contain "a key and a value".

how can i deserialize my List<object>  in objects that will be accessible with getters ?


thank you in advance for your help
Erwin PombettAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
that work for me :
[DataContract()]
        public class PositionIns
        {
            private List<PositionOnMap> _data;

            public PositionIns()
            {
                
            }

            public PositionIns(List<PositionOnMap> data)
            {
                this._data = data;
            }

            [DataMember(Name = "list")]
            public List<PositionOnMap> Data
            {
                get { return _data; }
                set { _data = value; }
            }
        }


        [DataContract()]
        public class PositionOnMap
        {
            private string name_;
            private string valLatLng_;

            public PositionOnMap(string name, string valposition)
            {
                name_       = name;
                valLatLng_  = valposition;
            }

            [DataMember(Name = "name")]
            public string Name
            {
                get { return name_; }
                set { name_ = value; }
            }

            [DataMember(Name = "value")]
            public string ValPosition
            {
                get { return valLatLng_; }
                set { valLatLng_ = value; }
            }
        }

Open in new window

0
 
leakim971PluritechnicianCommented:
0
 
Erwin PombettAuthor Commented:
hello leakim971,
sorry for the delay....holydays ;)

thank you for your help,

but i can not succeed to get the values out of my List<object>,

can you help me on my code as follow ?

----- string in json sent to webservice  -------
 {"list":[{"name":"5_28_139","value":"5_28_139(46.19731236038988, 6.094872275390571)"},{"name":"8_28_142","value":"8_28_142(46.18306210568767, 6.099002148437535)"},{"name":"14_28_116","value":"14_28_116(46.20802512160607, 6.104505312500123)"},{"name":"35_28_117","value":"35_28_117(46.179991902568105, 6.139877556152214)"}]}

------ webservice method ------
  [WebMethod(EnableSession = true)] // todo:: ajouter : Description="do smthg"
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SetLorriesNewPosition(List<object> list)
        {

              PositionOnMap pos = new PositionOnMap();
             DataContractJsonSerializer srlzr = new DataContractJsonSerializer(pos.GetType());

                foreach(PositionOnMap aLorryPos in list)
                {
                    string name = aLorryPos.Name;
                    string value = aLorryPos.ValPosition;
                }
       }catch(Exception ex)
            {
                theLog_.Info("MapElementsConfiguration.SetLorriesNewPosition: error:" + ex.Message);    
            }
            return _result.ToString();
        }

------- class for contract ---------
 [DataContract()]
        public class PositionOnMap
        {
            private string name_;
            private string valLatLng_;

            [DataMember(Name="name")]
            public string Name  
            {
                get { return name_;  }
                set { name_ = value; }
            }
           
            [DataMember(Name="valposition")]
            public string ValPosition
            {
                get { return valLatLng_;  }
                set { valLatLng_= value; }
            }
        }


----- error received when debbuging:

Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'Ecowaste.Wasteplan.RS.Presentation.Map.PositionOnMap'.


note:  the difference i have is that you treat a string, i'd like to keep my List<object>  if possible,
thank you for further help.

toshi
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Erwin PombettAuthor Commented:
hello leakim,

i've followed the example and create a unitTest in order to get values out of the json string, but my data is always empty. what am i doing wrong ?

here's my test class:

    [TestFixture]
    public class LorriesPositionsTest
    {

        string jsonValue2 = @"{""list"":[
                            {""name"":""5_28_139"",""value"":""5_28_139(46.19731236038988, 6.094872275390571)""},
                            {""name"":""8_28_142"",""value"":""8_28_142(46.18306210568767, 6.099002148437535)""},
                            {""name"":""14_28_116"",""value"":""14_28_116(46.20802512160607, 6.104505312500123)""},
                            {""name"":""35_28_117"",""value"":""35_28_117(46.179991902568105, 6.139877556152214)""}]}";

        [Test]
        public void ReadJson()
        {
                PositionIns Checkins = new PositionIns();
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonValue2));
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(Checkins.GetType());

                Checkins = serializer.ReadObject(ms) as PositionIns;
                ms.Close();

            for (int i = 0; i <= Checkins.Data.Count - 1; i++)
            {
                string name = Checkins.Data.ElementAt(i).Name;
                string  id = Checkins.Data.ElementAt(i).ValPosition;
                // Do something with name and id
                Console.Write(i + " - " + name + " ; id" + id);
            }
        }
        /*
        for (i = 0; i <= Checkins.Data.Count - 1; i++) {
          dynamic name = Checkins.Data.ElementAt(i).Name;
          dynamic id = Checkins.Data.ElementAt(i).Id;
          // Do something with name and id
        }
         */






        [DataContract()]
        public class PositionIns
        {
            private List<PositionOnMap> _data;
            private Paging              _paging;

            public PositionIns()
            {
               
            }

            public PositionIns(List<PositionOnMap> data, Paging paging)
            {
                this._data = data;
                this._paging = paging;
            }

            [DataMember(Name = "data")]
            public List<PositionOnMap> Data
            {
                get { return _data; }
                set { _data = value; }
            }


            [DataMember(Name = "paging")]
            public Paging Paging
            {
                get { return _paging; }
                set { _paging = value; }
            }
        }


        [DataContract()]
        public class PositionOnMap
        {
            private string name_;
            private string valLatLng_;

            public PositionOnMap(string name, string valposition)
            {
                name_       = name;
                valLatLng_  = valposition;
            }

            [DataMember(Name = "name")]
            public string Name
            {
                get { return name_; }
                set { name_ = value; }
            }

            [DataMember(Name = "valposition")]
            public string ValPosition
            {
                get { return valLatLng_; }
                set { valLatLng_ = value; }
            }
        }



        [DataContract()]
        public class Paging
        {
            private string _next;
            public Paging()
            {
            }

            public Paging(string nxt)
            {
                this._next = nxt;
            }

            [DataMember(Name = "next")]
            public string Nxt
            {
                get { return _next; }
                set { _next = value; }
            }

        }

    }
0
 
leakim971PluritechnicianCommented:
Hello toshi_,

The DataMember Name attribute must match with the key of the JSON object.

Best Regards.
0
 
Erwin PombettAuthor Commented:
hello leakim971,

i've succeed to recover my values with the follwoing loop:

  foreach (object aLorryPos in list)
                {
                    string lorryLatLng   = ((Dictionary<string, object>)aLorryPos)["value"].ToString();
                    string lorryName     = ((Dictionary<string, object>)aLorryPos)["name"].ToString();
                }

but i'm interested on using the way you answer in the post you gave me the link to.

is it possible to use your "way" with my param : "List<object>",
btw, what Datamemeber must match? on my positionIns ?

thank you in advance for further help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.