我正在尝试搜索此JSON代码以查找统计信息:
{
"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的tatpalsessionswon。我尝试访问数据如何从其他JSON文件访问数据,但它不允许我指定Champion I的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#的新手,因此特别感谢您的帮助!
如果Newtonsoft.Json;
对您来说是新的使用人类可读的文本来传输由属性 - 价值对组成的数据对象。从这种字符串中获取数据将与数据库一样容易。
首先要轻松地从JSON字符串中获取数据,我们需要使JSON字符串的对象显示为传承结构,因此我们需要查看数据或JSON字符串的外观。因此,如果您看到最高级别的继承结构包含summonerId
,modifyDate
和champions
champions
内部可能会有n
冠军的详细信息,因此我们创建了champions
现在,您可以看到冠军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; }
}
现在,由于我们已经获得了结构,因此我们需要 deserialize 将字符串到我们类型为rootObject的对象。那将将正常的JSON字符串转换为填充对象。现在只能获取像吃蛋糕一样的细节。
using Newtonsoft.Json;
Rootobject rt = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
if(rt.champions[1].id == 36)
{
Console.WriteLine(rt.champions[1].stats.totalSessionsWon);
}
当问题的作者试图使用JObject
查询其JSON对象时,我想我会使用相同的解决方案。
JObject
用于与Linq查询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"];
}