【发布时间】:2016-12-17 12:19:08
【问题描述】:
我有一个这样的 JSON API,
{
"pokemon": {
"currentPokemon": 1,
"total": 1,
"totalCount": 1,
},
"collections": [
{
"pokemonId": 2310,
"pokemonName": "Pikachu",
"pokemonType": "Land",
"status": {
"Active": "YES",
"Holder": "ASH"
},
"power": {
"Type": 10,
"name": "Thunder"
},
}
]
}
我有这些 API 的 C# 类
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 类中的值,例如 pokemonoId pokemonName等等!
帮助我度过难关!
【问题讨论】:
-
deserializedObject.Collections是List<Collection>;它没有名为PokemonId的属性。你的意思是deserializedObject.Collections[0].PokemonId? -
是的!你是对的
-
您的代码中还有其他几个拼写错误。请发布确切的编译错误。
-
错误 CS1061 'List
' 不包含 'pokemonId' 的定义,并且没有扩展方法 'pokemonId' 接受第一个参数可以找到“List ”类型的(您是否缺少 using 指令或程序集引用?)