1

we have a json serializer and deserializer downloaded, it reads the profile object find, but its not putting in the Client item in the list. Here is the json

{"Profile": [{
                "Name":"Joe",
                "Last :"Doe",
                 "Client":
                         {
                          "ClientId":"1",
                          "Product":"Apple",
                          "Message":"Peter likes apples"
                          },
                  "Date":"2012-02-14"
                 }]}

So in my profile class i have

public class Profile
  {
     public string Name {get; set;}
     public string Last {get; set;}
     public List<Client> Client {get; set;}
     public DateTime dDate {get; set;}   

        public Profile()
        {
        }
        public Profile BuildEntity()
        {
          Profile profile = new Profile();
          profile.Name = this.Name;
          profile.Last = this.LastName;
          profile.Client = this.client;
          profile.dDate = this.dDate;
          return dDate;

        }
  }

Now, when I debug all the items have values except for the list. Does anyone know what it might be?

NOTE: This is being posted to our Profile.asmx web service

Regards

user710502
  • 11,181
  • 29
  • 106
  • 161
  • Good job on leaving out the one relevent peice of code. Also, what kind of a question is `"Does anyone know what it might be?"` Mebe it's a giraffe. – Sam Axe Apr 03 '12 at 14:43
  • Boo, thank you for your note, I am new working with JSON and web services. I would be more than happy to provide more information. What piece is the relevant piece of code? No need to be rude :) – user710502 Apr 03 '12 at 14:56
  • What's with the `dDate` vs `Date`? Also, what JSON library are you using? – Roy Dictus Apr 03 '12 at 15:01
  • LB that helped me to get to this point thanks, I am not sure why its not mapping to the Client – user710502 Apr 03 '12 at 15:09
  • @user710502 I posted [here](http://stackoverflow.com/a/9988494/932418) a completely working code. What is wrong with it? – L.B Apr 03 '12 at 15:22

3 Answers3

1

Maybe client should be an array, not an object because un you model it is a List. try with this:

              "Client":
                     [{
                      "ClientId":"1",
                      "Product":"Apple",
                      "Message":"Peter likes apples"
                      }],
JAiro
  • 5,914
  • 2
  • 22
  • 21
1

You declared Client as:

public List<Client> Client {get; set;}

But your data looks like this:

"Client":
{
    "ClientId":"1",
    "Product":"Apple",
    "Message":"Peter likes apples"
}

I think the data expected is more like:

"Client":
[{
    "ClientId":"1",
    "Product":"Apple",
    "Message":"Peter likes apples"
}]

The de-serialization is probably expecting an array of objects, rather than just an object.

slashingweapon
  • 11,007
  • 4
  • 31
  • 50
1

In the future I would recommend using LinqPad to test with and then implement. Below is the working code sample.


    string JASON = @"
        {""Profile"": [{
                        ""Name"":""Joe"",
                        ""Last"":""Doe"",
                        ""Client"":
                                {
                                ""ClientId"":""1"",
                                ""Product"":""Apple"",
                                ""Message"":""Peter likes apples""
                                },
                        ""Date"":""2012-02-14""
                        }]}
    ";
    void Main()
    {
        var jason = JsonConvert.SerializeObject(Container.Instance());
        JASON.Dump();
        jason.Dump();
        JsonConvert.DeserializeObject(JASON).Dump();
    }

    // Define other methods and classes here
    class Container
    {
        public Container()
        {
            Profile = new List { };
        }
        public List Profile { get; set; }

        public static Container Instance()
        {
            var c = new Container();
            c.Profile.Add(
                new Profile {
                    Name = "Joe",
                    Last = "Doe",
                    Date = "2012-02-14",
                    Client = new Client{ ClientId = 1, Product = "Apple", Message = "Peter likes apples" }
            });
            return c;
        }
    }

    class Client
    {
        public int ClientId { get; set; }
        public string Product { get; set; }
        public string Message { get; set; }
    }

    class Profile
    {
        public string Name {get; set;}
        public string Last {get; set;}
        public Client Client {get; set;}
        public string Date {get; set;}
        public Profile()
        { }
    }

Max
  • 458
  • 3
  • 7