Parse data from multiple json arrays in c# - c#

I am trying to search this Json Code to find statistics:
{
"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
}
}]
}
In the following example, I want to be able search for totalSessionsWon for id 36. I tried accessing the data how I have been accessing data from other JSON files but it doesn't allow me to specify the id of the champion I am searching for:
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"];
Is there a way that I could choose a specific statistic based on the id before it?
I'm new to using both JSON and C#, so your help is especially appreciated!

If Newtonsoft.Json; is new for you than you can have a look at how to install Newtonsoft now once the installation is done i would love to tell you that XML, JSON are an open-standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. The fetching of the data from this kind of strings would be as easier as database.
For fetching the data from the json string with ease first we need to make the object of the json string which shows the heirarchy and thus we need to see how our data or json string looks like. So if you see the top most level of the heirarchy contains summonerId, modifyDate and champions Inside champions there could be n number of champion details so we created the list of champion as champions
now one level down of the heirarchy you can see the champion id and his stats so stats would be one more class to create about the champion. so your class would look like
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; }
}
Now since we already got the structure we need to Deserialize the string to the object of our type that is Rootobject. That will convert that normal json string to fill in the objects. Now just fetch the details like eating a cake.
using Newtonsoft.Json;
Rootobject rt = JsonConvert.DeserializeObject<Rootobject>(jsonstr);
if(rt.champions[1].id == 36)
{
Console.WriteLine(rt.champions[1].stats.totalSessionsWon);
}

As the question's author was attempting to use JObject to query their JSON object, I thought I would give a solution using the same.
JObject is for querying JSON with Linq. Linq is a semi-advanced subject for new C# programmers to get their head around but in short, is a specialised query language for retrieving data from a data source. The method outlined in Mohit Shrivastrava's answer is much easier for new programmers to get their head around.
//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"];
}

Related

Saving JSON to DataTable

I need to save data retrieved from API to a DataTable. JSON which is returned from API can't be deserialized directly to DataTable using this code:
DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
I got an error: Unexpected JSON token when reading DataTable. I read that it's beacuse JSON format is not as it should be. Mine is as follows:
{
"page": 1,
"page_size": 1000,
"items": [
{
"id": "e1b019b9a8bf408c9cb964c29e845104",
"asset_id": "5adb0d87882b4e14b99bde74a967e84c",
"alias": "Concrete Pump Yellow",
"serial_number": "QEQ000123",
"model": {
"name": "Pump C50-HP"
},
"operating_hours": {
"hours": 100,
"unit_driven": true
}
}
]
}
I know I need format like [{..}] but can't find workaround, API returns JSON as above. I can deserialize it using this:
var obj = JsonConvert.DeserializeObject(json);
but how can I now add data to DataTable? I'm looking for a solution for it
What the JsonConvert class does is it materializes your string version of the response into an object. For this to work, your string version has to match the structure of the resulting object or the class needs hints to know how to inflate the object. The runtime is telling you that there is a mismatch and it doesn't know how to resolve it.
There are a few ways to get this done. I prefer an structured approach so I would recommend you create classes to receive the data:
var payload = #"{
""page"": 1,
""page_size"": 1000,
""items"": [
{
""id"": ""e1b019b9a8bf408c9cb964c29e845104"",
""asset_id"": ""5adb0d87882b4e14b99bde74a967e84c"",
""alias"": ""Concrete Pump Yellow"",
""serial_number"": ""QEQ000123"",
""model"": {
""name"": ""Pump C50-HP""
},
""operating_hours"": {
""hours"": 100,
""unit_driven"": true
}
}
]
}";
public class ApiResponse
{
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("page_size")]
public int PageSize { get; set; }
[JsonProperty("items")]
public IEnumerable<ApiResponseItem> Items { get; set; }
}
public class ApiResponseItem
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("asset_id")]
public string AssetId { get; set; }
[JsonProperty("alias")]
public string Alias { get; set; }
[JsonProperty("serial_number")]
public string SerialNumber { get; set; }
[JsonProperty("model")]
public ApiResponseModel Model { get; set; }
[JsonProperty("operating_hours")]
public ApiResponseOperatingHours OperatingHours { get; set; }
}
public class ApiResponseModel
{
[JsonProperty("name")]
public string Name { get; set; }
}
public class ApiResponseOperatingHours
{
[JsonProperty("hours")]
public string Hours { get; set; }
[JsonProperty("unit_driven")]
public bool UnitDriven { get; set; }
}
var response = JsonConvert.DeserializeObject<ApiResponse>(payload);
As you can see, the classes use hint attributes to let the deserializer know about the fields. You can then loop through the response.Items enumerable and consume the items as desired.
UPDATE:
For posterity and at the suggestion of #mason, it's important to point out that there is no need to use a DataTable. A quick inspection of the payload reveals the output is a paged version of set of records so it's not equivalent to a data table.
Your issue here is that the json you're deserializing is not a DataTable, its just an Object.
JsonConvert.DeserializeObject(request, typeof(Object)) -> Where Object would be a defined Class with parameter definitions to deserialize the json to, i.e page, page_size, id etc..
Once in this format its fairly easy to coerce it into a DataTable:
https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=net-6.0
The Classes would look something along the lines of:
public class Items
{
public Guid? Id {get;set;}
public Guid? AssetId {get;set;}
public string alias {get;set;}
public string serial_number {get;set;}
public Model model {get;set;}
public OperatingHours operatingHours {get;set;}
}
public class Model
{
public string Name { get;set;}
}
public class OperatingHours
{
public int Hours {get;set;}
public bool Unit_Driven {get;set;}
}
public class OverallObject
{
public int Page {get;set;}
public int PageSize {get;set;}
public List<Items> AllItems {get;set;}
}

Deserializing Many Json Objects into List - C# [duplicate]

I have this JSON and I cannot figure out how to convert it to a List of objects in C#.
Here is the JSON:
{
"2": {
"sell_average": 239,
"buy_average": 238,
"overall_average": 240,
"id": 2
},
"6": {
"sell_average": 184434,
"buy_average": 182151,
"overall_average": 189000,
"id": 6
},
"8": {
"sell_average": 11201,
"buy_average": 1723,
"overall_average": 180,
"id": 8
}
}
And the code I've tried using:
public class ItemSummaryModel
{
public string Id { get; set; }
public ItemSummary ItemSummary { get; set; }
}
public class ItemSummary
{
public int Sell_Average { get; set; }
public int Buy_Average { get; set; }
public int Overall_Average { get; set; }
public int Id { get; set; }
}
List<ItemSummaryModel> models =
JsonConvert.DeserializeObject<List<ItemSummaryModel>>(jsonSummary);
to no avail. How can I deserialize this JSON into lists of these objects using Newtonsoft's JSON library (Json.Net)?
You can use
var dict = JsonConvert.DeserializeObject<Dictionary<int, ItemSummary>>(json);
var items = dict.Values.ToList(); //if you want a List<ItemSummary>;
public class ItemSummaryModel
{
[JsonProperty(PropertyName = "2")]
public ItemSummary Two { get; set; }
[JsonProperty(PropertyName = "6")]
public ItemSummary Six { get; set; }
[JsonProperty(PropertyName = "8")]
public ItemSummary Eight { get; set; }
}
var result = JsonConvert.DeserializeObject<ItemSummaryModel>(json);
This will technically work to get you a complex object, but I don't much like it. Having numbers for property names is going to be an issue. This being json for a single complex object will also be tricky. If it were instead a list, you could begin writing your own ContractResolver to handle mapping the number property name to an id field and the actual object to whatever property you wanted.

Read JSON Array that contains Arrays(?) C#

I apologize in advance for the poor explanation of my problem.
I'm trying to read a JSON array that contains data.
Here's how the JSON looks:
{
"playerstats": {
"steamID": "76561198071680006",
"gameName": "GameName",
"achievements": [
{
"apiname": "AchievementName1",
"achieved": 0, <--- Data that I want to read
"unlocktime": 0
},
{
"apiname": "AchievementName2",
"achieved": 0, <--- Data that I want to read
"unlocktime": 0
},
{
"apiname": "AchievementName2",
"achieved": 1, <--- Data that I want to read
"unlocktime": 1477847680
}
]
,
"success": true
}
}
I'm trying to look at Newtonsoft.Json and how to use that yet I'm at a complete loss as to how to use this. Any help would be appreciated.
You can try JsonConvert.DeserializeObject to read json data.
Having
public class Rootobject
{
public Playerstats playerstats { get; set; }
}
public class Playerstats
{
public string steamID { get; set; }
public string gameName { get; set; }
public Achievement[] achievements { get; set; }
public bool success { get; set; }
}
public class Achievement
{
public string apiname { get; set; }
public int achieved { get; set; }
public int unlocktime { get; set; }
}
You can try this:
var filePath = path to your file;
var jsonData = System.IO.File.ReadAllText(filePath);
Rootobject objectValue =
Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(jsonData);
Note: you should remove <--- Data that I want to read parts from your json data because currently it is not a valid json format.
You may access to your desired properties like this
objectValue.playerstats.achievements[0].achieved //0
objectValue.playerstats.achievements[1].achieved //0
objectValue.playerstats.achievements[2].achieved //1

C# Non-standard JSON parsing

I have an oddly formulated JSON response string in this format:
{
"Result": <this is the array of Ticket objects>,
"IsLastPage": true,
"NextSkip": 1,
"NextTake": 1,
"PageCount": 2,
"TotalCount": 3,
"QueryResultHash": "sample string 4"
}
Usually I would access the Json array (the Result value above) when the array is the only thing being returned, like so:
var jsonArray = JArray.Parse(resultString);
foreach (var jsonObject in jsonArray)
{ ... }
But I am not sure how to break down the above string so that I can get the 7 values individually and parse the array. Any suggestions?
If possible, I would use the library Newtonsoft.Json (https://www.nuget.org/packages/Newtonsoft.Json/).
Then you can create a ResponseContainer class. Something like,
//generated by http://json2csharp.com/
public class ResponseContainer
{
public List<object> Result { get; set; }
public bool IsLastPage { get; set; }
public int NextSkip { get; set; }
public int NextTake { get; set; }
public int PageCount { get; set; }
public int TotalCount { get; set; }
public string QueryResultHash { get; set; }
}
Then you can do
JsonSerializer serializer = new JsonSerializer();
ResponseContainer response = serializer.Deserialize<ResponseContainer>(jsonString);
Now you can access the fields in the json response as a C# object.

Getting an exception when trying to deserialize JSON and display the results in a DataGridView

I have some C# code where I get JSON data from an API. The JSON looks like this:
{
"count": 32696,
"results": [{
"data_id": 0,
"name": "Extended Potion of Ghost Slaying",
"rarity": 0,
"restriction_level": 0,
"img": "",
"type_id": 0,
"sub_type_id": 0,
"price_last_changed": "2013-03-18 17:00:31 UTC",
"max_offer_unit_price": 0,
"min_sale_unit_price": 0,
"offer_availability": 0,
"sale_availability": 0,
"sale_price_change_last_hour": 0,
"offer_price_change_last_hour": 0
}]
}
(There is more than just one item in the results though.)
I have made 2 classes like this:
internal class MyClass
{
public int data_id { get; set; }
public string name { get; set; }
public int rarity { get; set; }
public int restriction_level { get; set; }
public string img { get; set; }
public int type_id { get; set; }
public int sub_type_id { get; set; }
public string price_last_changed { get; set; }
public int max_offer_unit_price { get; set; }
public int min_sale_unit_price { get; set; }
public int offer_availability { get; set; }
public int sale_availability { get; set; }
public int sale_price_change_last_hour { get; set; }
public int offer_price_change_last_hour { get; set; }
}
internal class RootObject
{
public int count { get; set; }
public List<MyClass> results { get; set; }
}
And here is the part where I get the JSON and deserialize it:
using (WebClient wc = new WebClient())
{
string URI = "a good url";
wc.Headers.Add("Content-Type", "text");
string HtmlResult = wc.DownloadString(URI);
MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(HtmlResult);
DataTable dt = (DataTable)JsonConvert.DeserializeObject(HtmlResult, (typeof(DataTable)));
this.dataGridView1.DataSource = dt;
}
But when I run this code I get an error:
Additional information: Cannot deserialize the current JSON object
(e.g. {"name":"value"}) into type 'gwspiderv2.MyClass[]' because the
type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
I already use this type of code on another API without errors. What am I doing wrong?
In your code it seems you are trying to deserialize the same JSON two different ways, which doesn't make a whole lot of sense:
MyClass[] result = JsonConvert.DeserializeObject<MyClass[]>(HtmlResult);
DataTable dt = (DataTable)JsonConvert.DeserializeObject(HtmlResult, (typeof(DataTable)));
You are getting the first error (in your question) because your JSON represents a single object, but you are trying to deserialize it into an array of MyClass. You have defined a RootObject class, but you are not using it. It seems that you should be, because it fits your JSON.
You are getting the second error (in the comments to #inan's answer) because the JSON is in the wrong format to be deserialized into a DataTable. Presumably you are trying to do that so you can display the data in your DataGridView. But you don't need to convert it to a DataTable in order to use it as a data source. You can just give your DataGridView an IList, which you already have in your RootObject.
Change your code to this:
RootObject result = JsonConvert.DeserializeObject<RootObject>(HtmlResult);
this.dataGridView1.DataSource = result.results;
Use RootObject for deserializing as below, it has the List of MyClass
RootObject result = JsonConvert.DeserializeObject<RootObject>(HtmlResult);

Categories

Resources