无法将JSON值反序列化为Type <t>

时间:2016-01-15 17:05:26

标签: c# json api rest

我的类型是报告:

 [DataContract]
internal class Report
{
    [DataMember]
    internal string created_at;

    internal string Created_at
    {
        get { return created_at; }
        set { created_at = value; }
    }

    [DataMember]
    internal string eligible_id;

    internal string Eligible_id
    {
        get { return eligible_id; }
        set { eligible_id = value; }
    }
    [DataMember]
    private int[] known_issues;

    public int[] Known_issues
    {
        get { return known_issues; }
        set { known_issues = value; }
    }

    [DataMember]
    internal string response_code;

    internal string Response_code
    {
        get { return response_code; }
        set { response_code = value; }
    }

    [DataMember]
    internal string response_description;

    internal string Response_description
    {
        get { return response_description; }
        set { response_description = value; }
    }

    [DataMember]
    internal string agency_qualifier_code;

    internal string Agency_qualifier_code
    {
        get { return agency_qualifier_code; }
        set { agency_qualifier_code = value; }
    }

    [DataMember]
    internal string agency_qualifier_description;

    internal string Agency_qualifier_description
    {
        get { return agency_qualifier_description; }
        set { agency_qualifier_description = value; }
    }

    [DataMember]
    internal string reject_reason_code;

    internal string Reject_reason_code
    {
        get { return reject_reason_code; }
        set { reject_reason_code = value; }
    }

    [DataMember]
    internal string reject_reason_description;

    internal string Reject_reason_description
    {
        get { return reject_reason_description; }
        set { reject_reason_description = value; }
    }

    [DataMember]
    internal string follow_up_action_code;

    internal string Follow_up_action_code
    {
        get { return follow_up_action_code; }
        set { follow_up_action_code = value; }
    }

    [DataMember]
    internal string folow_up_description;

    internal string Folow_up_description
    {
        get { return folow_up_description; }
        set { folow_up_description = value; }
    }

    [DataMember]
    internal string details;

    internal string Details
    {
        get { return details; }
        set { details = value; }
    }
}     

当我尝试从API调用中获取JSON对象时,我得到:

enter image description here

当我尝试反序列化此对象并将其放在我的类型报告中时,我不断获取空白值,就好像我的报告对象中没有放置任何内容一样。那是为什么?

public static Tuple<string, string, bool> eligible(string ptFirst, string ptLast, string ptPolicy, string DOB, string DOS, string payerID)
    {            
        string apiSecret = "##### ###### #######";
        string providerOrgName = "###############################";
        string ptPolicyID = ptPolicy;
        string ptFirstName = ptFirst;
        string ptLastName = ptLast;
        string ptDOB = Convert.ToDateTime(DOB).ToString("yyyy-MM-dd");
        string ptDOS = Convert.ToDateTime(DOS).ToString("yyyy-MM-dd");
        string serviceType = "18";
        string NPI = "#####################";       

        RestClient client = new RestClient("https://api.com/v1.5/coverage/all?api_key="
            + apiSecret + "&payer_id=" + payerID + "&service_provider_organization_name=" + providerOrgName + "&provider_npi=" + NPI +
            "&member_id=" + ptPolicyID + "&member_first_name=" + ptFirstName + "&member_last_name=" + ptLastName +
            "&member_dob=" + ptDOB + "&date=" + DOS + "&service_type=" + serviceType + "&format=REST");

        var request = new RestRequest(Method.GET);
        IRestResponse response = client.Execute(request);
        string response271 = response.Content;   
>>>>    //This is the JSON object from my first picture         
        MessageBox.Show(response271);
        JavaScriptSerializer js = new JavaScriptSerializer();
        Report result = new Report();            
        result = js.Deserialize<Report>(response271); 
>>>>    //This is the box that comes up blank, my second picture.
        MessageBox.Show(result.eligibility_id);

        return Tuple.Create("test", "test", false);          
 }
  

当我打电话给这一行时

Messagebox.Show(result.eligiblity_id);

  

我得到一个空白的字符串。

enter image description here

我是使用JSON对象的新手,但我一直试图解决这个问题两天,所以我希望有人可以指导我从这里开始。

1 个答案:

答案 0 :(得分:1)

你可以试试Json.Net。

Change result = js.Deserialize<Report>(response271);

result = JsonConvert.DeserializeObject<Report>(response271);

编辑以回答后续问题

Link to Json.Net site

错误是json响应中的对象,因此您需要添加:

[JsonProperty]
public Error error {get;set;}

到您的报告类并制作错误类:

public class Error
{
    [JsonProperty]
    internal string response_code;

    internal string Response_code
    {
        get { return response_code; }
        set { response_code = value; }
    }

    [JsonProperty]
    internal string response_description;

    internal string Response_description
    {
        get { return response_description; }
        set { response_description = value; }
    }

    [JsonProperty]
    internal string agency_qualifier_code;

    internal string Agency_qualifier_code
    {
        get { return agency_qualifier_code; }
        set { agency_qualifier_code = value; }
    }

    [JsonProperty]
    internal string agency_qualifier_description;

    internal string Agency_qualifier_description
    {
        get { return agency_qualifier_description; }
        set { agency_qualifier_description = value; }
    }

    [JsonProperty]
    internal string reject_reason_code;

    internal string Reject_reason_code
    {
        get { return reject_reason_code; }
        set { reject_reason_code = value; }
    }

    [JsonProperty]
    internal string reject_reason_description;

    internal string Reject_reason_description
    {
        get { return reject_reason_description; }
        set { reject_reason_description = value; }
    }

    [JsonProperty]
    internal string follow_up_action_code;

    internal string Follow_up_action_code
    {
        get { return follow_up_action_code; }
        set { follow_up_action_code = value; }
    }

    [JsonProperty]
    internal string folow_up_description;

    internal string Folow_up_description
    {
        get { return folow_up_description; }
        set { folow_up_description = value; }
    }

    [JsonProperty]
    internal string details;

    internal string Details
    {
        get { return details; }
        set { details = value; }
    }
}     

这将像result.error.details

一样访问