将 JSON C# 类 调用到方法中
Call JSON C# classes into a method
我有一个这样的JSONAPI,
{
"pokemon": {
"currentPokemon": 1,
"total": 1,
"totalCount": 1,
},
"collections": [
{
"pokemonId": 2310,
"pokemonName": "Pikachu",
"pokemonType": "Land",
"status": {
"Active": "YES",
"Holder": "ASH"
},
"power": {
"Type": 10,
"name": "Thunder"
},
}
]
}
我有 C# 类 用于 API
Public ClassPokemonster
{
public class RootObject
{
[JsonProperty("pokemon")]
public Pokemon Pokemon { get; set; }
[JsonProperty("collections")]
public List<Collection> Collections { get; set; }
}
public class Pokemon
{
[JsonProperty("currentPokemon")]
public int CurrentPokemon { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
[JsonProperty("totalCount")]
public int TotalCount { get; set; }
}
public class Collection
{
[JsonProperty("pokemonId")]
public int PokemonId { get; set; }
[JsonProperty("pokemonName")]
public string PokemonName { get; set; }
[JsonProperty("pokemonType")]
public string PokemonType { get; set; }
[JsonProperty("status")]
public Status Status { get; set; }
[JsonProperty("power")]
public Power Power { get; set; }
}
public class Status
{
[JsonProperty("Active")]
public string Active { get; set; }
[JsonProperty("Holder")]
public string Holder { get; set; }
}
public class Power
{
[JsonProperty("Type")]
public int Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
}
我正在尝试使用此方法断言那些与 API 值匹配的值
Driver.Instance.Navigate().GoToUrl(url);
//WebRequest
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
getRequest.Method = "GET";
var getResponse = (HttpWebResponse)getRequest.GetResponse();
Stream newStream = getResponse.GetResponseStream();
StreamReader sr = new StreamReader(newStream);
//Deserialize JSON results
var result = sr.ReadToEnd();
Pokemonster deserializedObjects = JsonConvert.DeserializeObject<Pokemonster>(result);
我试图以这种方式断言,
Assert.Equal("2310", deserializedObject.Collections.PokemonId.ToString());
我的断言不获取 collections
class 中的值,例如 pokemonoId
pokemonName
等等!
帮我度过难关!
第一个问题(这可能只是您在此处设置格式的问题,但为了完整起见我应该提及)是您有:
Public ClassPokemonster
但正确的语法是:
public class Pokemonster
接下来,请注意所有其他 class 都在 class Pokemonster
中声明。这种结构称为nested type。按照您的设计方式,Pokemonster
class 本身不包含任何属性或方法,但嵌套的 classes Pokemonster.RootObject
、Pokemonster.Pokemon
等包含有属性。所以为了正确反序列化这个类型,你必须使用:
Pokemonster.RootObject deserializedObjects =
JsonConvert.DeserializeObject<Pokemonster.RootObject>(result);
最后,注意 属性,Pokemonster.RootObject.Collections
实际上有类型 List<Pokemonster.Collection>
,但是 List<T>
没有任何 属性 命名为 PokemonId
(因此出现错误消息)。您必须访问此列表中的项目才能获取其任何属性,如下所示:
Assert.Equal("2310", deserializedObject.Collections[0].PokemonId.ToString());
我有一个这样的JSONAPI,
{
"pokemon": {
"currentPokemon": 1,
"total": 1,
"totalCount": 1,
},
"collections": [
{
"pokemonId": 2310,
"pokemonName": "Pikachu",
"pokemonType": "Land",
"status": {
"Active": "YES",
"Holder": "ASH"
},
"power": {
"Type": 10,
"name": "Thunder"
},
}
]
}
我有 C# 类 用于 API
Public ClassPokemonster
{
public class RootObject
{
[JsonProperty("pokemon")]
public Pokemon Pokemon { get; set; }
[JsonProperty("collections")]
public List<Collection> Collections { get; set; }
}
public class Pokemon
{
[JsonProperty("currentPokemon")]
public int CurrentPokemon { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
[JsonProperty("totalCount")]
public int TotalCount { get; set; }
}
public class Collection
{
[JsonProperty("pokemonId")]
public int PokemonId { get; set; }
[JsonProperty("pokemonName")]
public string PokemonName { get; set; }
[JsonProperty("pokemonType")]
public string PokemonType { get; set; }
[JsonProperty("status")]
public Status Status { get; set; }
[JsonProperty("power")]
public Power Power { get; set; }
}
public class Status
{
[JsonProperty("Active")]
public string Active { get; set; }
[JsonProperty("Holder")]
public string Holder { get; set; }
}
public class Power
{
[JsonProperty("Type")]
public int Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
}
我正在尝试使用此方法断言那些与 API 值匹配的值
Driver.Instance.Navigate().GoToUrl(url);
//WebRequest
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
getRequest.Method = "GET";
var getResponse = (HttpWebResponse)getRequest.GetResponse();
Stream newStream = getResponse.GetResponseStream();
StreamReader sr = new StreamReader(newStream);
//Deserialize JSON results
var result = sr.ReadToEnd();
Pokemonster deserializedObjects = JsonConvert.DeserializeObject<Pokemonster>(result);
我试图以这种方式断言,
Assert.Equal("2310", deserializedObject.Collections.PokemonId.ToString());
我的断言不获取 collections
class 中的值,例如 pokemonoId
pokemonName
等等!
帮我度过难关!
第一个问题(这可能只是您在此处设置格式的问题,但为了完整起见我应该提及)是您有:
Public ClassPokemonster
但正确的语法是:
public class Pokemonster
接下来,请注意所有其他 class 都在 class Pokemonster
中声明。这种结构称为nested type。按照您的设计方式,Pokemonster
class 本身不包含任何属性或方法,但嵌套的 classes Pokemonster.RootObject
、Pokemonster.Pokemon
等包含有属性。所以为了正确反序列化这个类型,你必须使用:
Pokemonster.RootObject deserializedObjects =
JsonConvert.DeserializeObject<Pokemonster.RootObject>(result);
最后,注意 属性,Pokemonster.RootObject.Collections
实际上有类型 List<Pokemonster.Collection>
,但是 List<T>
没有任何 属性 命名为 PokemonId
(因此出现错误消息)。您必须访问此列表中的项目才能获取其任何属性,如下所示:
Assert.Equal("2310", deserializedObject.Collections[0].PokemonId.ToString());