0

以下代码尝试读取一些 json 并填充对象:

public Response ParseObject(string Json)
    {


        Response response = new Response();
        JsonConvert.PopulateObject(Json, response);
        return response;


    }

这是响应对象:

 public class Response
{
    public string id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string status { get; set; }
    public string incorporationDate { get; set; }
    public string latestAnnualReturnDate { get; set; }
    public string latestAccountsDate { get; set; }
    public string companyType { get; set; }
    public string accountsType { get; set; }

不幸的是,对象(响应)是空的,即(response.id 与所有其他属性一样为空)。

我猜我需要传入一些JsonSerializerSettings但我在任何地方都找不到教程?

4

2 回答 2

1

您可能想检查您的 Json 字符串。当我使用以下设置运行您的代码时,我会在 Response 对象中获得值。

        var s = "{ \"id\":\"2\" , \"name\":\"Doe\" }";
        Response response = ParseObject(s); 
于 2013-06-20T11:49:24.840 回答
0

响应尝试使用上述。这是从HttpContext

  Stream dataStream = context.Request.InputStream;
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            Response  data =(Response )json_serializer.DeserializeObject(responseFromServer);

在您的情况下,您可以使用

 JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            Response  data =(Response )json_serializer.DeserializeObject(Json);
于 2013-06-20T09:40:11.523 回答