2016-11-24 88 views
2

我试图寻找此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 
     } 
    }] 
} 

在下面的例子,我想成为totalSessionsWon能够搜索ID 36.我试着访问数据如何我已经从其他JSON文件访问数据,但它不允许我指定的冠军,我搜索的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#,所以你的帮助特别感谢!

+3

你不能摆动一个死亡的负鼠在这里没有击中JSON的问题。阅读其中的一些内容,试试看,并在卡住时发布实际问题 – Plutonix

+0

JSON的结构看起来很好。看起来您需要了解有关JSON的更多信息以及如何使用C#读取/写入它。如果您在网络上搜索该主题,可以找到关于该主题的大量教程。如果你只是在寻找教程,他们是关于堆栈溢出[关闭主题](http://stackoverflow.com/help/on-topic)。但是,如果您发布当前尝试的代码,那么我们可以帮助您找出所需数据出错的位置。 – Adrian

+1

@Plutonix那个负鼠对你做了什么? –

回答

3

如果Newtonsoft.Json;对你来说比你现在看到的如何install Newtonsoft现在一旦安装完成,我很想告诉你,XML,JSON是一种开放标准格式,使用人类可读的文本来传输数据对象由属性值对组成。从这种字符串中获取数据将像数据库一样简单。

对于从json字符串轻松获取数据首先我们需要使显示jira字符串的json字符串的对象成为可能,因此我们需要查看数据或json字符串的外观。所以,如果你看到的层次结构的最顶层包含summonerIdmodifyDatechampionschampions有可能是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; } 
} 

现在,因为我们已经得到了我们需要反序列化串到我们的类型的对象,它是Rootobject结构。这将转换该普通的json字符串来填充对象。现在只需获取细节,如吃蛋糕。

using Newtonsoft.Json; 

Rootobject rt = JsonConvert.DeserializeObject<Rootobject>(jsonstr); 
if(rt.champions[1].id == 36) 
{ 
    Console.WriteLine(rt.champions[1].stats.totalSessionsWon); 
} 
+0

考虑到这个问题的作者是JSON和C#的新手,最好包含一些关于这个代码的解释解决了用户需要的而不仅仅是提供解决方案。 (换句话说,解释代码中发生了什么) – Adrian

+1

希望@Adrian这个解释就够了。 :) –

+0

是的,我认为你已经做了一个体面的尝试来解释如何解决这个问题。 – Adrian

2

随着问题的作者试图用JObject来查询自己的JSON对象,我想我会给使用相同的解决方案。

JObject用于查询与Linq的JSON。对于新C#程序员来说,Linq是一个半高级主题,可以让他们轻松掌握,但简而言之,它是一种用于从数据源检索数据的专用查询语言。 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"]; 
}