locked
How to deserialize json string which contains list inside json. RRS feed

  • Question

  • User849221614 posted

    How to deserialize below json string to generic list .

    Error : Invalid object passed in, member name expected.  {"id":"0",""}'

    Passed below json in Poster :

    {

        "id": "1",
        "lstMobileNo": [
            {
                "MobileNo": "99988XXXXX"
            },
            {
                "MobileNo": "89899XXXXX"
            }
        ]
    }

    I am using below code for deserilize above json

     public GroupobjectResult AddUserToMyGroup(int id, List<MobileNo> mobileno)

    {

    UserDetail ud = json_der.Deserialize<UserDetail>("{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }");

    }

    And In Interface i have defined below class

     public class UserDetail
        {
            [DataMember]
            public string Id{ get; set; }

            [DataMember]
            public List<MobileNo> lstMobileNo { get; set; }
    }

        [DataContract]
        public class MobileNo
        {
            [DataMember]
            public string MobileNo {get;set;}

        }

    Saturday, March 15, 2014 2:49 AM

Answers

  • User1208776063 posted

    UserDetail ud = json_der.Deserialize<UserDetail>("{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }");

    Why are you deserializing the json string when you can directly create an instance of UserDetail with method parameters?

    UserDetail ud = new UserDetail() { Id = id.ToString(), lstMobileNo = mobileno };
    

    If you want to deserialize the example JSON you posted, you can do something like this

    string json = @"{
                    ""id"": ""1"",
                        ""lstMobileNo"": [
                            {
                                ""MobileNo"": ""99988XXXXX""
                            },
                            {
                                ""MobileNo"": ""89899XXXXX""
                            }
                        ]
                    }";
    UserDetail ud = (new JavaScriptSerializer()).Deserialize<UserDetail>(json);

    Also, you will not be able to use MobileNo as ClassName and PropertyName. You will get build error as 'MobileNo': member names cannot be the same as their enclosing type.

    [DataContract]
    public class MobileNo
    {
        [DataMember]
        public string mobileNo { getset; }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 16, 2014 4:53 AM
  • User-417640953 posted

    public GroupobjectResult AddUserToMyGroup(int id, List<MobileNo> mobileno)

    {

    UserDetail ud = json_der.Deserialize<UserDetail>("{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }");

    }

    Hi samarth88,

    Thank you post the issue to asp.net forum.

    As other member mentioned, we cannot Deserialize the string "{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }".

    The mobileno is the List<MobileNo> object, above string is not the json string you want.

    What I suggest you change the method AddUserToMyGroup as below.

    public GroupobjectResult AddUserToMyGroup(string jsonStr)
    
    {
    
    UserDetail ud = json_der.Deserialize<UserDetail>(jsonStr);
    
    }
    

    Then you should post a json string to server from client. If client is browser, you can refer to below.

     <script>
            var json = { "id": "1", "lstMobileNo": [{ "MobileNo": "99988XXXXX" }, { "MobileNo": "89899XXXXX" }] };
            var jsonstr = JSON.stringify(json);
            //post the jsonstr to server..
    
     </script>

    If the post code in the c# code behind you can make the json string like below in above post.

    string json = @"{
                    ""id"": ""1"",
                        ""lstMobileNo"": [
                            {
                                ""MobileNo"": ""99988XXXXX""
                            },
                            {
                                ""MobileNo"": ""89899XXXXX""
                            }
                        ]
                    }";
    

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 18, 2014 1:34 AM

All replies

  • User1208776063 posted

    UserDetail ud = json_der.Deserialize<UserDetail>("{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }");

    Why are you deserializing the json string when you can directly create an instance of UserDetail with method parameters?

    UserDetail ud = new UserDetail() { Id = id.ToString(), lstMobileNo = mobileno };
    

    If you want to deserialize the example JSON you posted, you can do something like this

    string json = @"{
                    ""id"": ""1"",
                        ""lstMobileNo"": [
                            {
                                ""MobileNo"": ""99988XXXXX""
                            },
                            {
                                ""MobileNo"": ""89899XXXXX""
                            }
                        ]
                    }";
    UserDetail ud = (new JavaScriptSerializer()).Deserialize<UserDetail>(json);

    Also, you will not be able to use MobileNo as ClassName and PropertyName. You will get build error as 'MobileNo': member names cannot be the same as their enclosing type.

    [DataContract]
    public class MobileNo
    {
        [DataMember]
        public string mobileNo { getset; }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 16, 2014 4:53 AM
  • User-417640953 posted

    public GroupobjectResult AddUserToMyGroup(int id, List<MobileNo> mobileno)

    {

    UserDetail ud = json_der.Deserialize<UserDetail>("{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }");

    }

    Hi samarth88,

    Thank you post the issue to asp.net forum.

    As other member mentioned, we cannot Deserialize the string "{\"id\":\"" + id+ "\",\"lstMobileNo\":\"" + mobileno+ "\" }".

    The mobileno is the List<MobileNo> object, above string is not the json string you want.

    What I suggest you change the method AddUserToMyGroup as below.

    public GroupobjectResult AddUserToMyGroup(string jsonStr)
    
    {
    
    UserDetail ud = json_der.Deserialize<UserDetail>(jsonStr);
    
    }
    

    Then you should post a json string to server from client. If client is browser, you can refer to below.

     <script>
            var json = { "id": "1", "lstMobileNo": [{ "MobileNo": "99988XXXXX" }, { "MobileNo": "89899XXXXX" }] };
            var jsonstr = JSON.stringify(json);
            //post the jsonstr to server..
    
     </script>

    If the post code in the c# code behind you can make the json string like below in above post.

    string json = @"{
                    ""id"": ""1"",
                        ""lstMobileNo"": [
                            {
                                ""MobileNo"": ""99988XXXXX""
                            },
                            {
                                ""MobileNo"": ""89899XXXXX""
                            }
                        ]
                    }";
    

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 18, 2014 1:34 AM