【问题标题】:How to save json attribute in some c# variables [duplicate]如何在一些c#变量中保存json属性[重复]
【发布时间】:2015-08-29 23:12:06
【问题描述】:

我编写了一个使用football-data.org 提供的API 的代码,我设法下载了包含我希望收到的所有参数的json。除非responseText中的内容,现在我想把responseText的内容分成一些变量。

string requestUrl = "http://api.football-data.org/alpha/soccerseasons/?season=2014";
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";

string responseText;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
    responseText = responseStream.ReadToEnd();
} 

Console.WriteLine(responseText);

您可以看到文档中也显示了 json 的结构,如下所示:

Example response:

{
"_links": {
   "self": { "href": "http://api.football-data.org/alpha/soccerseasons/354" },
   "teams": { "href": "http://api.football-data.org/alpha/soccerseasons/teams" },
   "fixtures": { "href": "http://api.football-data.org/alpha/soccerseasons/fixtures" },
   "leagueTable": { "href": "http://api.football-data.org/alpha/soccerseasons/leagueTable" }
},
 "caption": "Premier League 2014/15",
 "league": "PL",
 "year": "2014",
 "numberOfTeams": 20,
 "numberOfGames": 380,
 "lastUpdated": "2014-12-21T10:47:43Z"
}

然后我将创建一旦在 responseText 中获得内容的变量,全部拆分为:

String caption = the caption of json so (Premier League 2014/15)
String league = PL

我不知道我是否明确了想法,我编写的代码是否良好。我依靠我在 vb.net strong 方面的经验,现在我正在尝试迁移到 c# 以进行学习。

【问题讨论】:

  • 尝试搜索“C# parse json”或“C# deserialize json to object”。

标签: c# .net json


【解决方案1】:

您可以轻松地将 JSON 解析为对象。结构看起来像这样(使用json2csharp 生成,我会稍微编辑一下,因为有一些重复的代码):

public class Self
{
    public string href { get; set; }
}

public class Teams
{
    public string href { get; set; }
}

public class Fixtures
{
    public string href { get; set; }
}

public class LeagueTable
{
    public string href { get; set; }
}

public class Links
{
    [JsonProperty("self")]
    public Self Self { get; set; }
    [JsonProperty("teams")]
    public Teams Teams { get; set; }
    [JsonProperty("fixtures")]
    public Fixtures Fixtures { get; set; }
    [JsonProperty("leagueTable")]
    public LeagueTable LeagueTable { get; set; }
}

public class RootObject
{
    [JsonProperty("_links")]
    public Links Links { get; set; }
    [JsonProperty("caption")]
    public string Caption { get; set; }
    [JsonProperty("League")]
    public string League { get; set; }
    [JsonProperty("Year")]
    public string Year { get; set; }
    [JsonProperty("numberOfTeams")]
    public int NumberOfTeams { get; set; }
    [JsonProperty("NumberOfGames")]
    public int NumberOfGames { get; set; }
    [JsonProperty("LastUpdated")]
    public string LastUpdated { get; set; }
}

然后在你将内容读取为字符串后,使用诸如Json.NET之类的序列化框架,将其解析为一个对象:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(responseText);
Console.WriteLine(obj.Caption);

【讨论】:

  • 好的,谢谢您的回答。我已经安装了 Json.Net 框架,但是在您的这行代码中出现异常: RootObject obj = JsonConvert.DeserializeObject(responseText);特别是编译器告诉我:该类型需要一个 JSON 对象才能正确反序列化。
  • 确切的错误信息是什么?
  • Newtonsoft.Json.dll 中“Newton.Json.JsonSeriealizationException”类型的未处理异常附加信息:无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“测试”。 MainWindow RootObject +' 因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化。
  • 您是否正在尝试反序列化 RootObject 的数组?因为我在您的示例 JSON 中没有看到任何列表。也许试试JsonConvert.DeserializeObject&lt;List&lt;RootObject&gt;&gt;(responseText);
猜你喜欢
  • 2020-09-27
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多