2

我在 C# 应用程序中使用 HttpClient 来访问 Web API。我是这种 Web API 的新手(我通常做 WCF 服务)。

其中一个响应如下所示:

{
    "access_token": the access token,
    "scope": "read",
    "expires_in": seconds to expiry,
    "refresh_token": a refresh token
}

模型类如下所示:

class AuthResponse
{
    public string access_token { get; set; }
    public string scope { get; set; }
    public int expires_in { get; set; }
    public string refresh_token { get; set; }
}

所以当我这样做时:

var result = resposne.Content.ReadAsAsync<AuthResponse>().Result;

我得到了一个 AuthResponse 对象,其中填充了它的值。魔术。

下一位 API 返回如下响应:

{
    "data": {
         "visible": boolean,
         "email": valid e-mail string or null,
         "location_string": human-readable location identifier string,
         "ad_id": primary key of the ad
    }, 
    "actions": {
         "change_form": URL to change this ad
         "public_view": URL to view this ad's public HTML page
         "html_edit": URL to view this ad's HTML edit page 
                      that has more options than the API change_form does
    }
}

我的模型课会是什么样子?我如何解释嵌套?

4

1 回答 1

1

如果你现在还没有解决这个问题,这就是我发现的:

我发现这很棒:

http://json2csharp.com

您只需将您的 JSON url 甚至您的 JSON 粘贴到其中,它就会返回一个带有嵌套的模型类。或者,您可以在 Visual Studio 中复制您的 JSOn 和 Edit > Paste Special >,这也将返回您的模型类。

于 2013-12-06T08:34:49.693 回答