在使用Web服务时,如何知道通过JSON返回的数据类型?

时间:2016-10-14 11:14:26

标签: c# json serialization json.net

我以前没有使用JSON或使用Web服务的经验,但是我正在尝试使用返回气象信息的Web服务。

关于我正在尝试使用的API的

Here's the documentation

此API为我提供了使用JSON序列化的数据。我对JSON进行了一些阅读,根据我的理解,下载后访问此序列化数据的最佳方法是将其反序列化为具有匹配属性和类型的对象。我把这部分搞定了吗?但是我不明白在这种情况下我应该如何准确地知道通过JSON返回的数据的类型。

在我之前提到的API中,我在JSON中获得了API的响应示例:

{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}

我做的是,在Visual Studio上我使用了“Paste Special”> “粘贴为JSON类”选项,它为我创建了这些类:

public class Rootobject
{
    public Coord coord { get; set; }
    public Weather[] weather { get; set; }
    public string _base { get; set; }
    public Main main { get; set; }
    public Wind wind { get; set; }
    public Clouds clouds { get; set; }
    public Rain rain { get; set; }
    public int dt { get; set; }
    public Sys sys { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int cod { get; set; }
}

public class Coord
{
    public float lon { get; set; }
    public float lat { get; set; }
}

public class Main
{
    public float temp { get; set; }
    public int pressure { get; set; }
    public int humidity { get; set; }
    public float temp_min { get; set; }
    public float temp_max { get; set; }
}

public class Wind
{
    public float speed { get; set; }
    public int deg { get; set; }
}

public class Clouds
{
    public int all { get; set; }
}

public class Rain
{
    public int _3h { get; set; }
}

public class Sys
{
    public int type { get; set; }
    public int id { get; set; }
    public float message { get; set; }
    public string country { get; set; }
    public int sunrise { get; set; }
    public int sunset { get; set; }
}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

问题在于,当我使用HttpClient请求数据时,当我尝试反序列化响应时,我得到的关于数据类型不匹配的错误很少,例如,浮点数据被分配给int类型的属性。 / p>

以下是我的代码片段:

string json = await client.GetStringAsync("weather?q=London,uk&appid=010101010101010101101");
Rootobject currentWeather = new Rootobject();
currentWeather = JsonConvert.DeserializeObject<Rootobject>(json);
MessageBox.Show(currentWeather.name);

我理解在这种情况下,我可以更改我的类中的属性类型以匹配API返回的内容,但这对我来说感觉不对,主要是因为它似乎可能是麻烦和不可预知的行为。我这样做了吗?也许我在API文档中遗漏了一些内容,它们是否应该提供返回数据的类型?

2 个答案:

答案 0 :(得分:2)

正确:将其反序列化为具有匹配属性和类型的对象。

首先检查API文档中的类型,如果不够全面,我会考虑更改您的类型以匹配您从JSON推断的内容。

值289.9是浮点数。

值1435650870可以存储为int。

AU的值可以是字符串/枚举。

修改 我检查了您链接到的API文档,但没有看到它明确说明返回的数据类型。

编辑2: 更直接地回答你的问题,“我怎么能准确地知道通过JSON返回的数据的类型?”(感谢@CodeCaster),没有在文档中找到我认为你不能的信息

但我觉得只要查看返回的历史数据就可以获得99.999%的收益。

答案 1 :(得分:0)

如果您对使用动态感到满意,可以尝试使用此代码段

string json = await client.GetStringAsync("weather?q=London,uk&appid=010101010101010101101");
dynamic currentWeather= JObject.Parse(json);
MessageBox.Show(currentWeather.name);

您可以在documentation

中找到更多详情