1

我正在尝试反序列化 C# 中的 json 字符串。以下是我使用的 jsonstring 和类。但是在反序列化之后它给出了空值。那我怎么了?

 string jsonString = @"{'status':1,'data':[{'report_date':'2012-10-16','subid':'000ebc7d-d9fc-4822-923e-c7ac6027f5cd','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'0013eeb2-54e3-4ef1-9fff-9bc5c146858e','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'002c3efc-cf82-466e-a94b-994f548c27fc','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'003bac5b-81a7-4042-8e4c-b882c4d31534','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'00626129-c888-45c0-8401-c44f64d0e1c2','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'007da905-e053-48e5-800a-9815d3da6a34','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'ff8011fb-0dfd-4336-b3f3-0a915100767d','revenue':'0.00','clicks':'0'},{'report_date':'2012-10-16','subid':'ff855a9e-3f39-48fd-b27b-5eb15221acbd','revenue':'0.00','clicks':'0'}],'messages':[],'errors':[],'code':200,'time':{'click':1350456414}}";



     System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
     PM.AdknowledgeRevenue.Entities.RevenueList collection = serializer.Deserialize<PM.AdknowledgeRevenue.Entities.RevenueList>(jsonString);

我使用的课程

     public class Revenue
        {
            #region private members
            string _reportDate; 
            Guid subId; 
            Decimal revenue; 
            int clicks;
            int status;


            #endregion

            #region public members
            public int Status
            {
                get { return status; }
                set { status = value; }
            }
            public string ReportDate
            {
                get { return _reportDate; }
                set { _reportDate = value; }
            }
            public Guid SubId
            {
                get { return subId; }
                set { subId = value; }
            }
            public Decimal Revenue1
            {
                get { return revenue; }
                set { revenue = value; }
            }
            public int Clicks
            {
                get { return clicks; }
                set { clicks = value; }
            }
            #endregion
     }

    public class RevenueList
   {
     public IEnumerable<Revenue> RevenueCollection { get; set; }
   }

提前致谢

4

2 回答 2

3

我认为json格式不正确。字符串应该用双引号括起来。

您可以在此处验证您的 json 。

于 2012-10-17T07:13:31.050 回答
0

下面的代码有效..

var result = new JavaScriptSerializer().Deserialize<Result>(jsonString);

public class Result
{
    public int status;
    public List<Item> data;
}

public class Item
{
    public string report_date;
    public string subid;
    public string revenue;
    public string clicks;
}

或使用Json.Net

var result = JsonConvert.DeserializeObject<Result>(jsonString);

public class Result
{
    [JsonProperty("status")]
    public int Status;
    [JsonProperty("data")]
    public List<Item> Data;
}

public class Item
{
    [JsonProperty("report_date")]
    public string ReportDate;
    [JsonProperty("subid")]
    public string SubId;
    [JsonProperty("revenue")]
    public string Revenue;
    [JsonProperty("clicks")]
    public string Clicks;
}
于 2012-10-17T07:14:41.570 回答