1

My application is asp.net. I have to send some values back to server. For this I create a object serialize it and send it to server. At server I try to de-serialize it Following is my code

   [Serializable]
    public class PassData
    {
        public PassData()
        {  
        }

        public List<testWh> SelectedId { get; set; }

        public string SelectedControlClientId { get; set; }

        public string GroupTypeId { get; set; }

        public string SectionTypeId { get; set; }

  }


    [Serializable]
    public class testWh
    {
        public testWh()

        {
        }
        public string Id { get; set; }
    }


JavaScriptSerializer serializer = new JavaScriptSerializer();
//this can not serialize the SelectedId and the count remains 0
PassData data = serializer.Deserialize<PassData>(jsonString);
//this serialize in an anonymous object with key value pair
var data2 = serializer.DeserializeObject(textHiddenArguments.Text);

Following is my Json Serialized String

{
   "SelectedId":{"0":"ABCD","1":"JKLM"},
   "SelectedControlClientId":"YTUTOOO",
   "GroupTypeId":3,
   "SectionTypeId":"1"
}

quotes escaped string

"{\"SelectedId\":{\"0\":\"ABCD\",\"1\":\"JKLM\"},\"SelectedControlClientId\":\"YTUTOOO\",\"GroupTypeId\":3,\"SectionTypeId\":\"1\"}"

My Problem is Selected Id is array of testWH object. But when I try to desrialize it, the SelectedId property of PassData which is list does not get serialized and count remains zero.

I tried using array instead of List, which gave an exception "no parameter less constructor..."

Could any one explain the what I am doing wrong here ?

2
  • Not able to understand it. What do you mean by code-fields/field-initializers ? Commented May 18, 2012 at 7:06
  • ah, sorry - that was just my IDE add-in changing things when I pasted Commented May 18, 2012 at 7:08

1 Answer 1

2

The key problem here is that the JSON doesn't match the objects you have constructed. You can see this by writing the data you want and serializing:

var obj = new PassData
{
    SelectedId = new List<testWh>
    {
        new testWh { Id = "ABCD"},
        new testWh { Id = "JKLM"}
    },
    GroupTypeId = "3",
    SectionTypeId = "1",
    SelectedControlClientId = "YTUTOOO"
};
string jsonString = serializer.Serialize(obj);

which gives JSON like:

{"SelectedId":[{"Id":"ABCD"},{"Id":"JKLM"}],
 "SelectedControlClientId":"YTUTOOO","GroupTypeId":"3","SectionTypeId":"1"}

So now you need to decide which you want to change; the JSON or the classes. The following alternative class works fine with your original JSON, for example:

public class PassData
{
    public Dictionary<string,string> SelectedId { get; set; }
    public string SelectedControlClientId { get; set; }
    public string GroupTypeId { get; set; }
    public string SectionTypeId { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.