简体   繁体   中英

Can't deserialize this JSON into C# object

I have this JSON and would like to deserialize it into an object in C#. Notice the data field looks like a dictionary.

{
  "responseID": "LTg5MjUyNDgxMDgyNzU3NjgyNDMtMjAxNi0wNS0xOFQxOTo1ODoxOS45ODFaLTk4MTAwMDAwMA==",
  "timestamp": "2016-05-18T19:58:19.981Z",
  "type": "v0.1-reject-reason-codes",
  "context": {
    "tenant": "hpho"
  },
  "data": {
    "LOST_TO_COMPETITOR": "Lost to Competitor",
    "OTHER": "Other (see notes)",
    "DISCONTINUED": "Product Discontinued",
    "SERVICE_ISSUES": "Service Issues",
    "SEASONAL": "Seasonal Fluctuations"
  }
}

This is the class definition

public static class JSONConstants
    {
        public const string responseID = "responseID";
        public const string timestamp = "timestamp";
        public const string type = "type";
        public const string context = "context";
        public const string data = "data";
        public const string tenant = "tenant";
    }

    [DataContract]
    public class RejectReasonCodesResponse
    {
        [DataMember(Name = JSONConstants.responseID)]
        public string ResponseID { get; set; }
        [DataMember(Name = JSONConstants.timestamp)]
        public string Timestamp { get; set; }
        [DataMember(Name = JSONConstants.type)]
        public string Type { get; set; }
        [DataMember(Name = JSONConstants.context)]
        public SummaryContext Context { get; set; }
        [DataMember(Name = JSONConstants.data)]
        public Dictionary<string, string> Data { get; set; }
    }

    [DataContract]
    public class SummaryContext
    {
        [DataMember(Name = JSONConstants.tenant)]
        public string Tenant { get; set; }
    }

This is how I deserialize it

string requestURL = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

                HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
                request.Headers["Ocp-Apim-Subscription-Key"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode != HttpStatusCode.OK
                 && response.StatusCode != HttpStatusCode.NonAuthoritativeInformation)
                    throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));

                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RejectReasonCodesResponse));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());

However, the Data field in objResponse is empty. What did I do wrong?

Can you verify your class definition. I think it should look like this.

public class SummaryContext
{
    public string tenant { get; set; }
}

public class Data
{
    public string LOST_TO_COMPETITOR { get; set; }
    public string OTHER { get; set; }
    public string DISCONTINUED { get; set; }
    public string SERVICE_ISSUES { get; set; }
    public string SEASONAL { get; set; }
}

public class RejectReasonCodesResponse
{
    public string responseID { get; set; }
    public string timestamp { get; set; }
    public string type { get; set; }
    public Context context { get; set; }
    public Data data { get; set; }
}

Rather try use javascriptserializer like

javascriptserializer js = new javascriptserializer();
var response = js.Deserialize<RejectReasonCodesResponse>(jsonstring);

Firstly lint the JSON using a site like json.lint. When its green go into visual studio if you have it and paste special, you can paste json as classes. Website Json2csharp also useful.

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