JSON中的反序列化在所有数据成员中接收空值

时间:2017-01-11 10:36:27

标签: c# json rest serialization json-deserialization

以下是我的服务在调用时返回的有效JSON:

[{
   "CCRQ": "2006/1/26 0:00:00",
   "CLXH": "CA6510B1",
   "CarBodyColor": "color"
}]

以下是我的代码,其中包含datamembers:

[DataContract]
public class Response
{
    [DataMember(Name = "CCRQ")]
    public string CCRQ { get; set; }

    [DataMember(Name = "CLXH")]
    public string CLXH { get; set; }

    [DataMember(Name = "CarBodyColor")]
    public string CarBodyColor { get; set; }
}

但是我在所有数据成员中提出服务和接收null的请求。

public static Response MakeRequest(string requestUrl)
{
    HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));

        object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
        Response jsonResponse = objResponse as Response;
        return jsonResponse;
    }
}

为什么我在所有属性中都为null?

1 个答案:

答案 0 :(得分:1)

您的JSON是一个包含1个对象内容的数组。对于我所看到的,你的反序列化器需要一个对象。

尝试回复

{
   "CCRQ": "2006/1/26 0:00:00",
   "CLXH": "CA6510B1",
   "CarBodyColor": "color"
}

或对objResponse as List<Response>

施放回复