简体   繁体   中英

json deserialization in c#

I am trying to deserialize the json string in C#. Following is jsonstring and classes I used. But after deserialize it is giving null value. So what's going wrong with me?

 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);

Classes i used

     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; }
   }

Thanks in advance

I think the json is not in the right format. String should be wrapped in double quotes.

You can validate your json here .

Below code works..

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;
}

or using 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM