【问题标题】:Call JSON C# classes into a method将 JSON C# 类调用到方法中
【发布时间】: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.CollectionsList&lt;Collection&gt;;它没有名为PokemonId 的属性。你的意思是deserializedObject.Collections[0].PokemonId
  • 是的!你是对的
  • 您的代码中还有其他几个拼写错误。请发布确切的编译错误。
  • 错误 CS1061 'List' 不包含 'pokemonId' 的定义,并且没有扩展方法 'pokemonId' 接受第一个参数可以找到“List”类型的(您是否缺少 using 指令或程序集引用?)

标签: c# json json.net json-api


【解决方案1】:

第一个问题(可能只是您在此处格式化的问题,但为了完整起见,我应该提到它)是您有:

Public ClassPokemonster

但正确的语法是:

public class Pokemonster

接下来,请注意您的所有其他类都被声明为Pokemonster。这种结构称为nested type。按照您的设计方式,Pokemonster 类本身不包含属性或方法,但嵌套类 Pokemonster.RootObjectPokemonster.Pokemon 等确实具有属性。所以为了正确反序列化这种类型,你必须使用:

Pokemonster.RootObject deserializedObjects = 
    JsonConvert.DeserializeObject<Pokemonster.RootObject>(result);

最后,请注意属性Pokemonster.RootObject.Collections 实际上具有List&lt;Pokemonster.Collection&gt; 类型,但List&lt;T&gt; 没有任何名为PokemonId 的属性(因此出现错误消息)。您必须访问此列表中的项目才能获取它的任何属性,如下所示:

Assert.Equal("2310", deserializedObject.Collections[0].PokemonId.ToString());

【讨论】:

  • 谢谢!魔法奏效了!以及如何将反序列化的对象放入不同的类文件中?比如说,我必须将 assert 语句移到另一个类中,它也会引发错误!
  • 你的Pokemonster 类和它所有的嵌套类应该被移动到它自己的代码文件(Pokemonster.cs)中,与任何与之相关的测试完全分开。只需确保它在同一个命名空间中或在需要的地方添加适当的using 语句。如果您使用的是 Visual Studio,只需右键单击项目并选择 Add > Class... 即可为您提供所需的模板。
  • 没错!已经是这样了。我想将webRequest , deserialize Json resultsAsserts 分成不同的班级。意思是,我有三个不同的类,分别是 Pokemonster.csAPI WebRequestAsserts
猜你喜欢
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2020-03-10
相关资源
最近更新 更多