C# 在c语言中解析来自多个json数组的数据#
我正在尝试搜索此Json代码以查找统计信息:C# 在c语言中解析来自多个json数组的数据#,c#,json,json.net,C#,Json,Json.net,我正在尝试搜索此Json代码以查找统计信息: { "summonerId": 32033681, "modifyDate": 1403658807000, "champions": [{ "id": 40, "stats": { "totalSessionsPlayed": 1, "totalSessionsLost": 0, "totalSessionsWon": 1
{
"summonerId": 32033681,
"modifyDate": 1403658807000,
"champions": [{
"id": 40,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalChampionKills": 1,
"totalDamageDealt": 27006,
"totalDamageTaken": 9924,
"mostChampionKillsPerSession": 1,
"totalMinionKills": 17,
"totalDoubleKills": 0,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 2,
"totalGoldEarned": 8383,
"mostSpellsCast": 0,
"totalTurretsKilled": 2,
"totalPhysicalDamageDealt": 8957,
"totalMagicDamageDealt": 18049,
"totalFirstBlood": 0,
"totalAssists": 13,
"maxChampionsKilled": 1,
"maxNumDeaths": 2
}
},
{
"id": 36,
"stats": {
"totalSessionsPlayed": 1,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalChampionKills": 0,
"totalDamageDealt": 14267,
"totalDamageTaken": 7649,
"mostChampionKillsPerSession": 0,
"totalMinionKills": 33,
"totalDoubleKills": 0,
"totalTripleKills": 0,
"totalQuadraKills": 0,
"totalPentaKills": 0,
"totalUnrealKills": 0,
"totalDeathsPerSession": 5,
"totalGoldEarned": 3258,
"mostSpellsCast": 0,
"totalTurretsKilled": 0,
"totalPhysicalDamageDealt": 4992,
"totalMagicDamageDealt": 9165,
"totalFirstBlood": 0,
"totalAssists": 0,
"maxChampionsKilled": 0,
"maxNumDeaths": 5
}
}]
}
在下面的示例中,我希望能够搜索id为36的totalSessionsWon。我尝试访问数据,但无法指定我正在搜索的冠军的id:
string jsonInput = new WebClient().DownloadString(@usableurl); //Reads the JSON from the API
string usableJson = @"JObject.Parse(jsonInput)"; //converts the JSON from the API to a usable form
var usableJson["champions"]["stats"]["totalSessionWon"];
有没有一种方法可以让我根据之前的id选择一个特定的统计数据
我对使用JSON和C#都是新手,因此特别感谢您的帮助 If
Newtonsoft.Json
对您来说是新的,您现在可以了解如何在安装完成后使用它。我想告诉您,XML、JSON是一种开放的标准格式,它使用人类可读的文本来传输由属性-值对组成的数据对象。从这类字符串中提取数据将和数据库一样容易
为了方便地从json字符串中获取数据,我们首先需要创建json字符串的对象,该对象显示继承权,因此我们需要查看数据或json字符串的外观。因此,如果您看到继承权的最高级别包含召唤ID
,修改日期
和冠军
内部冠军
可能有n
冠军详细信息,因此我们将冠军列表创建为冠军
现在你可以看到冠军id和他的统计数据,所以统计数据将是关于冠军的另一个类。所以你们班看起来像
public class Rootobject
{
public int summonerId { get; set; }
public long modifyDate { get; set; }
public List<Champion> champions { get; set; }
}
public class Champion
{
public int id { get; set; }
public Stats stats { get; set; }
}
public class Stats
{
public int totalSessionsPlayed { get; set; }
public int totalSessionsLost { get; set; }
public int totalSessionsWon { get; set; }
public int totalChampionKills { get; set; }
public int totalDamageDealt { get; set; }
public int totalDamageTaken { get; set; }
public int mostChampionKillsPerSession { get; set; }
public int totalMinionKills { get; set; }
public int totalDoubleKills { get; set; }
public int totalTripleKills { get; set; }
public int totalQuadraKills { get; set; }
public int totalPentaKills { get; set; }
public int totalUnrealKills { get; set; }
public int totalDeathsPerSession { get; set; }
public int totalGoldEarned { get; set; }
public int mostSpellsCast { get; set; }
public int totalTurretsKilled { get; set; }
public int totalPhysicalDamageDealt { get; set; }
public int totalMagicDamageDealt { get; set; }
public int totalFirstBlood { get; set; }
public int totalAssists { get; set; }
public int maxChampionsKilled { get; set; }
public int maxNumDeaths { get; set; }
}
公共类根对象
{
public int calleerid{get;set;}
公共长修改日期{get;set;}
公共列表{get;set;}
}
公开课冠军
{
公共int id{get;set;}
公共统计数据{get;set;}
}
公共类统计
{
公共int TotalSessionPlayed{get;set;}
公共int TotalSessionLost{get;set;}
公共int totalSessionsWon{get;set;}
公共int totalChampionKills{get;set;}
公共int TotalDamageDealed{get;set;}
公共int TotalDamageTake{get;set;}
公共int-mostChampionKillsPerSession{get;set;}
公共int totalMinionKills{get;set;}
公共整数{get;set;}
公共整数totalTripleKills{get;set;}
公共整数totalQuadraKills{get;set;}
公共整数{get;set;}
公共int totalUnrealKills{get;set;}
公共int TotalDeath会话{get;set;}
公共整数{get;set;}
公共int mostSpellsCast{get;set;}
公共整数{get;set;}
公共int TotalPhysicalDamageDemanted{get;set;}
公共整数TotalMagicDamageDemand{get;set;}
公共整数totalFirstBlood{get;set;}
公共整数{get;set;}
public int maxChampionsKilled{get;set;}
public int maxNumDeath{get;set;}
}
现在,由于我们已经得到了结构,我们需要将字符串反序列化到类型为Rootobject的对象。这将转换普通json字符串以填充对象。现在只需要像吃蛋糕一样获取细节
using Newtonsoft.Json;
Rootobject rt = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
if(rt.champions[1].id == 36)
{
Console.WriteLine(rt.champions[1].stats.totalSessionsWon);
}
使用Newtonsoft.Json;
Rootobject rt=JsonConvert.DeserializeObject(jsonstr);
if(rt.champions[1].id==36)
{
Console.WriteLine(rt.champions[1].stats.totalSessionsWon);
}
当问题的作者试图使用JObject
来查询他们的JSON对象时,我想我会用同样的方法给出一个解决方案
JObject
用于使用查询JSON。Linq对于新的C#程序员来说是一门半高级的课程,但简而言之,它是一种专门用于从数据源检索数据的查询语言。Mohit Shrivastrava回答中概述的方法对于新程序员来说更容易理解
//converts the JSON from the API to a usable form
JObject usableJson = JObject.Parse(json);
// retrieve champion objects
JToken champions = usableJson["champions"];
// retrieve the champion desired object using the Linq FirstOrDefault method.
// This method will return the first object that matches the given query,
// or return null if it does not find a match.
JToken champion = champions.FirstOrDefault(c=> (int)c["id"] == 36);
if (champion != null)
{
// retrieve the stats object
JToken stats = champion["stats"];
// read the totalSessionsWon field from the object.
int totalSessionsWon = (int) stats["totalSessionsWon"];
}
JSON的结构看起来不错。看起来您需要进一步了解JSON以及如何使用C#读/写JSON。如果你在网上搜索的话,你会发现很多关于这个主题的教程。如果您只是在寻找教程,它们位于堆栈溢出上。但是,如果您发布当前尝试的代码,我们可以帮助您找出访问所需数据时出现的错误。@Proputix那只负鼠对您做过什么?@AsadSaeeduddin她忽略了右下方所有高评级链接。;)令人惊讶的是,一个问题怎么会变化如此之快。。。例如“有没有一种方法可以将统计数据移动到id下以便访问?”被编辑为“有没有一种方法可以根据id之前的id选择特定的统计数据?”。。。注意你的下一个问题@Dan:-)考虑到问题的作者对JSON和C#还不熟悉,最好包含一些关于此代码如何解决用户需要的问题的解释,而不仅仅是提供解决方案。(换句话说,解释代码中发生了什么)希望@Adrian这个解释足够了。:)是的,我认为你在解释这是如何解决这个问题上做了一次很好的尝试。