Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-21
6
Medium Priority
?
695 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:toshi_
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 37008930
0
 

Author Comment

by:toshi_
ID: 37068678
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
 

Author Comment

by:toshi_
ID: 37069019
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 83

Expert Comment

by:leakim971
ID: 37069287
Hello toshi_,

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

Best Regards.
0
 

Author Comment

by:toshi_
ID: 37069390
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
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 37070305
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question