Json没有将元素的子数组映射到我的对象

时间:2012-04-03 14:37:22

标签: c# asp.net json

我们下载了一个json序列化器和反序列化器,它读取了profile对象find,但它没有放入列表中的Client项。这是json

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

所以在我的个人资料班里我有

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;

        }
  }

现在,当我调试所有项目时,除列表外都有值。有谁知道它可能是什么?

注意:这已发布到我们的Profile.asmx网络服务

此致

3 个答案:

答案 0 :(得分:1)

也许客户端应该是一个数组,而不是一个对象,因为你建模它是一个List。试试这个:

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

答案 1 :(得分:1)

您将客户声明为:

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

但您的数据如下所示:

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

我认为预期的数据更像是:

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

反序列化可能需要一个对象数组,而不仅仅是一个对象。

答案 2 :(得分:1)

将来我会建议使用LinqPad进行测试,然后再实施。以下是工作代码示例。


    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()
        { }
    }