This question sounds very trivial but I couldn't find it on internet. Let's say I am getting response like following json
{
"status": 1,
"msg": "1 out of 2 Transactions Fetched Successfully",
"transaction_details": {
"f9605b13-c300-4d11-b": {
"mihpayid": "14019310624",
"txnid": "f9605b13-c300-4d11-b",
"mode": "UPI",
"status": "success",
"App_Name": "PhonePe"
},
"546576": {
"mihpayid": "Not Found",
"status": "Not Found"
}
}
}
My problem is How do I make a Model in C# (So that I can deserialize this response)?
I tried this one -
public class TransactionDetails
{
[JsonProperty("txnid")]
public string TransactionId;
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("mode")]
public string Mode { get; set; }
[JsonProperty("mihpayid")]
public string MiPayId { get; set; }
[JsonProperty("amt")]
public string amount { get; set; }
[JsonProperty("App_name")]
public string AppName { get; set; }
}
public class ResponseBody
{
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("msg")]
public string Message { get; set; }
[JsonProperty("transaction_details")]
public List<TransactionDetails> Transactions { get; set; }
}
I know the problem, Problem is "transaction_details" is not a List, It is object of object.
How do I model that, The number of Keys and name of the Keys are not known!!
Please help me!
and sorry if the question is too trivial.
Any time you are dealing with a JSON structure where the keys are not static (i.e. the key is a unique identifier, date/time, etc.) you will need to use a Dictionary. You will still have the benefit of using a strongly-typed value, and you can perform any validation operations on the key to handle different formats as necessary.
I followed the steps described on this tutorial. My case is a little bit different:
Instead of indexing Hotels and Rooms, I am indexing Candidates and Resumes.
Instead of using CosmosDB I am using an Azure SQL Database.
Following the tutorial, I am able to create the Index, the 2 Indexers (one for the SQL DB and one for the Blobs storage), and the 2 data sources.
The SQL DB contains all my candidates, and the storage contains all their resumes (files with PDF/DOC/DOCX formats). Each blob has a metadata "ResumeCandidateId" that contains the same value as the "CandidateId" for the Candidate.
I have the following fields for my Index:
[SerializePropertyNamesAsCamelCase]
public partial class Candidate
{
[Key]
[IsFilterable, IsRetrievable(true), IsSearchable]
public string CandidateId { get; set; }
[IsFilterable, IsRetrievable(true), IsSearchable, IsSortable]
public string LastName { get; set; }
[IsFilterable, IsRetrievable(true), IsSearchable, IsSortable]
public string FirstName { get; set; }
[IsFilterable, IsRetrievable(true), IsSearchable, IsSortable]
public string Notes { get; set; }
public ResumeBlob[] ResumeBlobs { get; set; }
}
[SerializePropertyNamesAsCamelCase]
public class ResumeBlob
{
[IsRetrievable(true), IsSearchable]
[Analyzer(AnalyzerName.AsString.StandardLucene)]
public string content { get; set; }
[IsRetrievable(true)]
public string metadata_storage_content_type { get; set; }
public long metadata_storage_size { get; set; }
public DateTime metadata_storage_last_modified { get; set; }
public string metadata_storage_name { get; set; }
[Key]
[IsRetrievable(true)]
public string metadata_storage_path { get; set; }
[IsRetrievable(true)]
public string metadata_content_type { get; set; }
public string metadata_author { get; set; }
public DateTime metadata_creation_date { get; set; }
public DateTime metadata_last_modified { get; set; }
public string ResumeCandidateId { get; set; }
}
As you can see, one Candidate can have multiple Resumes. The challenge is to populate the ResumeBlobs property...
The data from the SQL DB is indexed and mapped correctly by the Indexer. When I run the Blobs Indexer, it loads documents, however it does not map them and they never show up in the search (ResumeBlobs is always empty). Here is the code used to create the Blobs Indexer:
var blobDataSource = DataSource.AzureBlobStorage(
name: "azure-blob-test02",
storageConnectionString: "DefaultEndpointsProtocol=https;AccountName=yyy;AccountKey=xxx;EndpointSuffix=core.windows.net",
containerName: "2019");
await searchService.DataSources.CreateOrUpdateAsync(blobDataSource);
List<FieldMapping> map = new List<FieldMapping> {
new FieldMapping("ResumeCandidateId", "CandidateId")
};
Indexer blobIndexer = new Indexer(
name: "hotel-rooms-blobs-indexer",
dataSourceName: blobDataSource.Name,
targetIndexName: indexName,
fieldMappings: map,
//parameters: new IndexingParameters().SetBlobExtractionMode(BlobExtractionMode.ContentAndMetadata).IndexFileNameExtensions(".DOC", ".DOCX", ".PDF", ".HTML", ".HTM"),
schedule: new IndexingSchedule(TimeSpan.FromDays(1)));
bool exists = await searchService.Indexers.ExistsAsync(blobIndexer.Name);
if (exists)
{
await searchService.Indexers.ResetAsync(blobIndexer.Name);
}
await searchService.Indexers.CreateOrUpdateAsync(blobIndexer);
try
{
await searchService.Indexers.RunAsync(blobIndexer.Name);
}
catch (CloudException e) when (e.Response.StatusCode == (HttpStatusCode)429)
{
Console.WriteLine("Failed to run indexer: {0}", e.Response.Content);
}
I commented the parameters for the blobIndexer but I get the same results even if it's not commented.
When I run a search, here is an example of what I get:
{
"#odata.context": "https://yyy.search.windows.net/indexes('index-test01')/$metadata#docs(*)",
"value": [
{
"#search.score": 1.2127206,
"candidateId": "363933d1-7e81-4ed2-b82e-d7496d98db50",
"lastName": "LAMLAST",
"firstName": "ZFIRST",
"notes": "MGA ; SQL ; T-SQL",
"resumeBlobs": []
}
]
}
"resumeBlobs" is empty. Any idea how to do such a mapping?
AFAIK, Azure Search doesn't support a collection merge feature that seems to be necessary to implement your scenario.
An alternative approach to this is to create a separate index for resumes and point the resume indexer to that index. That means that some of your search scenarios will have to hit two indexes, but it's a path forward.
I am trying to get the data from the Steam Store API. The goal is to pass it an AppID and then get the data for that game using the API and storing it in a Game object. I am fetching the data up until serialization. The below is the code I an using:
namespace GData
{
public class Rootobject
{
public Game _9050 { get; set; }
}
public class Game
{
public bool success { get; set; }
public Data data { get; set; }
}
public class Data
{
public string type { get; set; }
public string name { get; set; }
public int steam_appid { get; set; }
public int required_age { get; set; }
public bool is_free { get; set; }
public string detailed_description { get; set; }
public string about_the_game { get; set; }
public string short_description { get; set; }
public string supported_languages { get; set; }
public string header_image { get; set; }
public string website { get; set; }
public Pc_Requirements pc_requirements { get; set; }
public object[] mac_requirements { get; set; }
public object[] linux_requirements { get; set; }
public string[] developers { get; set; }
public string[] publishers { get; set; }
public Price_Overview price_overview { get; set; }
public int[] packages { get; set; }
public Package_Groups[] package_groups { get; set; }
public Platforms platforms { get; set; }
public Metacritic metacritic { get; set; }
public Category[] categories { get; set; }
public Genre[] genres { get; set; }
public Screenshot[] screenshots { get; set; }
public Recommendations recommendations { get; set; }
public Release_Date release_date { get; set; }
public Support_Info support_info { get; set; }
public string background { get; set; }
}
public class Pc_Requirements
{
public string minimum { get; set; }
}
public class Price_Overview
{
public string currency { get; set; }
public int initial { get; set; }
public int final { get; set; }
public int discount_percent { get; set; }
}
public class Platforms
{
public bool windows { get; set; }
public bool mac { get; set; }
public bool linux { get; set; }
}
public class Metacritic
{
public int score { get; set; }
public string url { get; set; }
}
public class Recommendations
{
public int total { get; set; }
}
public class Release_Date
{
public bool coming_soon { get; set; }
public string date { get; set; }
}
public class Support_Info
{
public string url { get; set; }
public string email { get; set; }
}
public class Package_Groups
{
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public string selection_text { get; set; }
public string save_text { get; set; }
public int display_type { get; set; }
public string is_recurring_subscription { get; set; }
public Sub[] subs { get; set; }
}
public class Sub
{
public int packageid { get; set; }
public string percent_savings_text { get; set; }
public int percent_savings { get; set; }
public string option_text { get; set; }
public string option_description { get; set; }
public string can_get_free_license { get; set; }
public bool is_free_license { get; set; }
public int price_in_cents_with_discount { get; set; }
}
public class Category
{
public int id { get; set; }
public string description { get; set; }
}
public class Genre
{
public string id { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public int id { get; set; }
public string path_thumbnail { get; set; }
public string path_full { get; set; }
}
}
Fetching JSON:
public class GameData
{
public Game GetGameData(int GameId)
{
var url = "https://store.steampowered.com/api/appdetails/?appids=" + GameId.ToString();
//Game GameData = _download_serialized_json_data<Game>(url);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
Game GameData = new Game();
GameData = JsonConvert.DeserializeObject<Game>(jsonString);
return GameData;
}
}
The code is returning success: false.
The issue seems to be in the deserialization as the jsonString = reader.ReadToEnd(); jsonString variable is getting full with the data.
Is there something wrong in the Game class?
I am using C# and the Newtonsoft JSON library.
Sample JSON retrieved in jsonString (https://store.steampowered.com/api/appdetails/?appids=570):
{
"570":{
"success":true,
"data":{
"type":"game",
"name":"Dota 2",
"steam_appid":570,
"required_age":0,
"is_free":true,
"dlc":[
859040
],
"detailed_description":"<strong>The most-played game on Steam.<\/strong><br>Every day, millions of players worldwide enter battle as one of over a hundred Dota heroes. And no matter if it's their 10th hour of play or 1,000th, there's always something new to discover. With regular updates that ensure a constant evolution of gameplay, features, and heroes, Dota 2 has truly taken on a life of its own.<br><br><strong>One Battlefield. Infinite Possibilities.<\/strong><br>When it comes to diversity of heroes, abilities, and powerful items, Dota boasts an endless array\u2014no two games are the same. Any hero can fill multiple roles, and there's an abundance of items to help meet the needs of each game. Dota doesn't provide limitations on how to play, it empowers you to express your own style.<br><br><strong>All heroes are free.<\/strong><br>Competitive balance is Dota's crown jewel, and to ensure everyone is playing on an even field, the core content of the game\u2014like the vast pool of heroes\u2014is available to all players. Fans can collect cosmetics for heroes and fun add-ons for the world they inhabit, but everything you need to play is already included before you join your first match.<br><br><strong>Bring your friends and party up.<\/strong><br>Dota is deep, and constantly evolving, but it's never too late to join. <br>Learn the ropes playing co-op vs. bots. Sharpen your skills in the hero demo mode. Jump into the behavior- and skill-based matchmaking system that ensures you'll <br>be matched with the right players each game.",
"about_the_game":"<strong>The most-played game on Steam.<\/strong><br>Every day, millions of players worldwide enter battle as one of over a hundred Dota heroes. And no matter if it's their 10th hour of play or 1,000th, there's always something new to discover. With regular updates that ensure a constant evolution of gameplay, features, and heroes, Dota 2 has truly taken on a life of its own.<br><br><strong>One Battlefield. Infinite Possibilities.<\/strong><br>When it comes to diversity of heroes, abilities, and powerful items, Dota boasts an endless array\u2014no two games are the same. Any hero can fill multiple roles, and there's an abundance of items to help meet the needs of each game. Dota doesn't provide limitations on how to play, it empowers you to express your own style.<br><br><strong>All heroes are free.<\/strong><br>Competitive balance is Dota's crown jewel, and to ensure everyone is playing on an even field, the core content of the game\u2014like the vast pool of heroes\u2014is available to all players. Fans can collect cosmetics for heroes and fun add-ons for the world they inhabit, but everything you need to play is already included before you join your first match.<br><br><strong>Bring your friends and party up.<\/strong><br>Dota is deep, and constantly evolving, but it's never too late to join. <br>Learn the ropes playing co-op vs. bots. Sharpen your skills in the hero demo mode. Jump into the behavior- and skill-based matchmaking system that ensures you'll <br>be matched with the right players each game.",
"short_description":"Every day, millions of players worldwide enter battle as one of over a hundred Dota heroes. And no matter if it's their 10th hour of play or 1,000th, there's always something new to discover. With regular updates that ensure a constant evolution of gameplay, features, and heroes, Dota 2 has truly taken on a life of its own.",
"supported_languages":"Bulgarian, Czech, Danish, Dutch, English<strong>*<\/strong>, Finnish, French, German, Greek, Hungarian, Italian, Japanese, Korean<strong>*<\/strong>, Norwegian, Polish, Portuguese, Portuguese-Brazil, Romanian, Russian, Simplified Chinese<strong>*<\/strong>, Spanish, Swedish, Thai, Traditional Chinese, Turkish, Ukrainian<br><strong>*<\/strong>languages with full audio support",
"reviews":"\u201cA modern multiplayer masterpiece.\u201d<br>9.5\/10 \u2013 <a href=\"https:\/\/www.destructoid.com\/review-dota-2-258506.phtml\" target=\"_blank\" rel=\"noreferrer\" >Destructoid<\/a><br><br>\u201cOnce you start to learn its secrets, there\u2019s a wild and exciting variety of play here that\u2019s unmatched, even by its peers.\u201d<br>9.4\/10 \u2013 <a href=\"http:\/\/www.ign.com\/articles\/2013\/07\/24\/dota-2-review\" target=\"_blank\" rel=\"noreferrer\" >IGN<\/a><br><br>\u201cDota 2 is possibly the only competitive free-to-play game that is totally uncompromised by its business model.\u201d<br>90\/100 \u2013 <a href=\"http:\/\/www.pcgamer.com\/dota-2-review-2\/\" target=\"_blank\" rel=\"noreferrer\" >PC Gamer<\/a><br>",
"header_image":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/header.jpg?t=1525818062",
"website":"http:\/\/www.dota2.com\/",
"pc_requirements":{
"minimum":"<strong>Minimum:<\/strong><br><ul class=\"bb_ul\"><li><strong>OS:<\/strong> Windows 7 or newer<br><\/li><li><strong>Processor:<\/strong> Dual core from Intel or AMD at 2.8 GHz<br><\/li><li><strong>Memory:<\/strong> 4 GB RAM<br><\/li><li><strong>Graphics:<\/strong> nVidia GeForce 8600\/9600GT, ATI\/AMD Radeon HD2600\/3600<br><\/li><li><strong>DirectX:<\/strong> Version 9.0c<br><\/li><li><strong>Network:<\/strong> Broadband Internet connection<br><\/li><li><strong>Storage:<\/strong> 15 GB available space<br><\/li><li><strong>Sound Card:<\/strong> DirectX Compatible<\/li><\/ul>"
},
"mac_requirements":{
"minimum":"<strong>Minimum:<\/strong><br><ul class=\"bb_ul\"><li><strong>OS:<\/strong> OS X Mavericks 10.9 or newer<br><\/li><li><strong>Processor:<\/strong> Dual core from Intel<br><\/li><li><strong>Memory:<\/strong> 4 GB RAM<br><\/li><li><strong>Graphics:<\/strong> nVidia 320M or higher, or Radeon HD 2400 or higher, or Intel HD 3000 or higher<br><\/li><li><strong>Network:<\/strong> Broadband Internet connection<br><\/li><li><strong>Storage:<\/strong> 15 GB available space<\/li><\/ul>"
},
"linux_requirements":{
"minimum":"<strong>Minimum:<\/strong><br><ul class=\"bb_ul\"><li><strong>OS:<\/strong> Ubuntu 12.04 or newer<br><\/li><li><strong>Processor:<\/strong> Dual core from Intel or AMD at 2.8 GHz<br><\/li><li><strong>Memory:<\/strong> 4 GB RAM<br><\/li><li><strong>Graphics:<\/strong> nVidia Geforce 8600\/9600GT (Driver v331), AMD HD 2xxx-4xxx (Driver mesa 10.5.9), AMD HD 5xxx+ (Driver mesa 10.5.9 or Catalyst 15.7), Intel HD 3000 (Driver mesa 10.6)<br><\/li><li><strong>Network:<\/strong> Broadband Internet connection<br><\/li><li><strong>Storage:<\/strong> 15 GB available space<br><\/li><li><strong>Sound Card:<\/strong> OpenAL Compatible Sound Card<\/li><\/ul>"
},
"developers":[
"Valve"
],
"publishers":[
"Valve"
],
"packages":[
197846
],
"package_groups":[
{
"name":"default",
"title":"Buy Dota 2",
"description":"",
"selection_text":"Select a purchase option",
"save_text":"",
"display_type":0,
"is_recurring_subscription":"false",
"subs":[
{
"packageid":197846,
"percent_savings_text":"",
"percent_savings":0,
"option_text":"Dota 2 - Commercial License - Free",
"option_description":"",
"can_get_free_license":"0",
"is_free_license":true,
"price_in_cents_with_discount":0
}
]
}
],
"platforms":{
"windows":true,
"mac":true,
"linux":true
},
"metacritic":{
"score":90,
"url":"http:\/\/www.metacritic.com\/game\/pc\/dota-2?ftag=MCD-06-10aaa1f"
},
"categories":[
{
"id":1,
"description":"Multi-player"
},
{
"id":9,
"description":"Co-op"
},
{
"id":29,
"description":"Steam Trading Cards"
},
{
"id":30,
"description":"Steam Workshop"
},
{
"id":40,
"description":"SteamVR Collectibles"
},
{
"id":35,
"description":"In-App Purchases"
},
{
"id":8,
"description":"Valve Anti-Cheat enabled"
}
],
"genres":[
{
"id":"1",
"description":"Action"
},
{
"id":"37",
"description":"Free to Play"
},
{
"id":"2",
"description":"Strategy"
}
],
"screenshots":[
{
"id":0,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_86d675fdc73ba10462abb8f5ece7791c5047072c.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_86d675fdc73ba10462abb8f5ece7791c5047072c.1920x1080.jpg?t=1525818062"
},
{
"id":1,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_ad8eee787704745ccdecdfde3a5cd2733704898d.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_ad8eee787704745ccdecdfde3a5cd2733704898d.1920x1080.jpg?t=1525818062"
},
{
"id":2,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_7ab506679d42bfc0c0e40639887176494e0466d9.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_7ab506679d42bfc0c0e40639887176494e0466d9.1920x1080.jpg?t=1525818062"
},
{
"id":3,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_c9118375a2400278590f29a3537769c986ef6e39.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_c9118375a2400278590f29a3537769c986ef6e39.1920x1080.jpg?t=1525818062"
},
{
"id":4,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_f9ebafedaf2d5cfb80ef1f74baa18eb08cad6494.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_f9ebafedaf2d5cfb80ef1f74baa18eb08cad6494.1920x1080.jpg?t=1525818062"
},
{
"id":5,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_27b6345f22243bd6b885cc64c5cda74e4bd9c3e8.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_27b6345f22243bd6b885cc64c5cda74e4bd9c3e8.1920x1080.jpg?t=1525818062"
},
{
"id":6,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_b33a65678dc71cc98df4890e22a89601ee56a918.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_b33a65678dc71cc98df4890e22a89601ee56a918.1920x1080.jpg?t=1525818062"
},
{
"id":7,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_d0f973ce376ca5b6c08e081cb035e86ced105fa9.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_d0f973ce376ca5b6c08e081cb035e86ced105fa9.1920x1080.jpg?t=1525818062"
},
{
"id":8,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_1f3b5f5ccf8b159294914c3fe028128a787304b6.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_1f3b5f5ccf8b159294914c3fe028128a787304b6.1920x1080.jpg?t=1525818062"
},
{
"id":9,
"path_thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_e0a92f15a6631a8186df79182d0fe28b5e37d8cb.600x338.jpg?t=1525818062",
"path_full":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/ss_e0a92f15a6631a8186df79182d0fe28b5e37d8cb.1920x1080.jpg?t=1525818062"
}
],
"movies":[
{
"id":256692021,
"name":"Dota 2 - Join the Battle",
"thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/256692021\/movie.293x165.jpg?t=1501892790",
"webm":{
"480":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/256692021\/movie480.webm?t=1501892790",
"max":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/256692021\/movie_max.webm?t=1501892790"
},
"highlight":true
},
{
"id":256692017,
"name":"Dota 2 - Sizzle Reel",
"thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/256692017\/movie.293x165.jpg?t=1501892798",
"webm":{
"480":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/256692017\/movie480.webm?t=1501892798",
"max":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/256692017\/movie_max.webm?t=1501892798"
},
"highlight":true
},
{
"id":2028243,
"name":"Dota 2 - The Greeviling",
"thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/2028243\/movie.293x165.jpg?t=1447357208",
"webm":{
"480":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/2028243\/movie480.webm?t=1447357208",
"max":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/2028243\/movie_max.webm?t=1447357208"
},
"highlight":false
},
{
"id":81026,
"name":"Dota 2 Gamescom Trailer",
"thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/81026\/movie.293x165.jpg?t=1501892804",
"webm":{
"480":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/81026\/movie480.webm?t=1501892804",
"max":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/81026\/movie_max.webm?t=1501892804"
},
"highlight":false
},
{
"id":2040250,
"name":"Dota 2 Reborn - Custom Games Are Here",
"thumbnail":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/2040250\/movie.293x165.jpg?t=1447376742",
"webm":{
"480":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/2040250\/movie480.webm?t=1447376742",
"max":"http:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/2040250\/movie_max.webm?t=1447376742"
},
"highlight":false
}
],
"recommendations":{
"total":911325
},
"release_date":{
"coming_soon":false,
"date":"9 Jul, 2013"
},
"support_info":{
"url":"http:\/\/dev.dota2.com\/",
"email":""
},
"background":"https:\/\/steamcdn-a.akamaihd.net\/steam\/apps\/570\/page_bg_generated_v6b.jpg?t=1525818062"
}
}
}
UPDATE: if I return Data datatype all value are null
public Data GetGameData(int GameId)
{
var url = "https://store.steampowered.com/api/appdetails/?appids=" + GameId.ToString();
//Game GameData = _download_serialized_json_data<Game>(url);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
var dict = JsonConvert.DeserializeObject<Dictionary<string, Data>>(jsonString);
Data gameData = dict[GameId.ToString()];
return gameData;
}
If I return Game datatype I get the same error:
public Game GetGameData(int GameId)
{
var url = "https://store.steampowered.com/api/appdetails/?appids=" + GameId.ToString();
//Game GameData = _download_serialized_json_data<Game>(url);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
var dict = JsonConvert.DeserializeObject<Dictionary<string, Game>>(jsonString);
Game gameData = dict[GameId.ToString()];
return gameData;
}
In your code, you are attempting to deserialize into a Game class containing success and data properties. However, in sample JSON, these properties are not at the root level. Instead, they are one level further down, inside a property called 570, which I am guessing is the game ID:
{
"570": {
"success": true,
"data": {
...
}
}
}
Since the class you are deserializing into doesn't match up with the JSON, you are getting default values (i.e. success is false and data is null).
To fix this, you will need something to wrap your Game class. And since the game ID will probably change depending on the request, you can't use a regular class like Rootobject. Instead you will need to deserialize into a Dictionary<string, Game> like this:
var dict = JsonConvert.DeserializeObject<Dictionary<string, Game>>(jsonString);
You can then retrieve the Game object from that:
Game gameData = dict[GameId.ToString()];
Here is a working demo: https://dotnetfiddle.net/ieT839
Note: In your classes, you can omit members that you don't really need. In playing around with different game ID's I discovered that the pc_requirements, mac_requirements and linux_requirements elements in the JSON can sometimes be an object and sometimes an array of objects. If you have your classes declared to expect one thing and the JSON returns the other, then you will get an error when deserializing. You can solve this problem using a custom JsonConverter like the SingleOrArrayConverter<T> from How to handle both a single item and an array for the same property using JSON.net. But if you don't really need those items, you can sidestep the issue just by commenting them out in your Data class.
This question already has answers here:
Deserialize JSON with C#
(10 answers)
Closed 5 years ago.
So I'm creating an endpoint using a data model called Chat that will accept data in this JSON form and store it in the database.
[{
"ID": "123456",
"Chat": [{
"ID": "1",
"Message": "User: that's a nice car Dylan: thanks",
"PostedBy": "Dylan",
"PostedOn": "2018-01-23T18:25:43.511Z"
},
{
"ID": "2",
"Message": "User: that's a really nice car Terry: thanks ",
"PostedBy": "Terry",
"PostedOn": "2018-02-23T18:25:43.511Z"
},
{
"ID": "3",
"Message": "User: that's the best car Roger: thanks",
"PostedBy": "Roger",
"PostedOn": "2018-03-23T18:25:43.511Z"
}
]
}]
This is what I have currently and when I send data to the endpoint it only stores the ID, and nothing else in the database. Any thoughts/guidance is appreciated on how I could alter my model to accept the entirety of the data that is being sent.
public class Chat
{
public string ID { get; set; }
public string message { get; set; }
public string postedBy { get; set; }
public DateTime? postedOn { get; set;}
}
I may be wrong here, but it seems to me like you're using a class that represents a single message-instance (the class Chat) to attempt to store a whole list of Chat-data.
If I'm right, the only reason it actually stores ID is that it by chance happens to have the same name for two different levels in your data; one for the outer list (the whole set - and this is what is stored), and one for each of the inner chat-items.
Try to add this class, and use that instead (or rather in addition, since it actually contains a list of instances of your already existing class Chat):
public class ChatThread
{
public string ID { get; set; }
public IEnumerable<Chat> Chat { get; set; }
}
The ID which is being stored on your object is not the ID from the Chat object, but rather the higher ID definition which is common to your Chat objects.
You're were really close, if we discount the fact that you have not taken into consideration that C# is a case-sensitive language.
The "higher" layer is composed of a String ID, but also of an array of Chat objects, so you should create a Class that holds the definition of these two properties.
public class JsonClass
{
public string ID { get; set; }
public Chat[] Chat { get; set; }
}
public class Chat
{
public string ID { get; set; }
public string Message { get; set; }
public string PostedBy { get; set; }
public DateTime PostedOn { get; set; }
}
Since there exist multiple Chat objects for the JsonClass ID property, you have to make it into a collection of some sort. I chose an array, but you can use other Collection objects, such as a List.
I'm trying to deserialize some Json using the JsonConvert. The example in JSON that I want to handle is:
2 {
3 'Name': 'Product 1',
4 'ExpiryDate': '2000-12-29T00:00Z',
5 'Price': 99.95,
6 'Sizes':{ 'Tall': 30
'ExtraTall':40
}
7 },
8 {
9 'Name': 'Product 2',
10 'ExpiryDate': '2009-07-31T00:00Z',
11 'Price': 12.50,
12 'Sizes': null
13 }
I already tried some stuff like:
var handlerLocal= JsonConvert.DeserializeObject <Dictionary <string,Dictionary<string,string>>>(z);
Till now I'm only handling simple lists using this:
var handlerLocal = JsonConvert.DeserializeObject<FriendsHandler>(z);
FriendHandler works like a model. But in this new case I have a List inside a List and that is killing me.
Anyone can help me?
Best regards
In general with JSON.net you can just write a object that matches what you're parsing correctly and then use DeserializeObject as you have.
In this case the JSON you have provided is invalid. It would be a dictionary in the case that it looked like this:
{'thing1': {
'Name': 'Product 1',
'ExpiryDate': '2000-12-29T00:00Z',
'Price': 99.95,
'Sizes':{ 'Tall': 30
'ExtraTall':40
}
},
'thing2': {
'Name': 'Product 2',
'ExpiryDate': '2009-07-31T00:00Z',
'Price': 12.50,
'Sizes': null
}}
I think it should be an array, which means it should be surrounded by [].
To parse this, just do this:
var handlerLocal = JArray.Parse(z).ToObject<List<FriendsHandler>>();
When trying to deserialise JSON, I find it best to use Json2Sharp.com to create classes to contain data, however the JSON you've provided is invalid so it's not possible to create a class from this.
If you could find some valid JSON to use, this tool will handle the rest for you.
Create a model like this:
public class Product
{
public string Name { get; set; }
public DateTime ExpiryDate { get; set; }
public decimal Price { get; set; }
public Dictionary<string, int> Sizes { get; set; }
}
Get your JSON
var productsJson = << Call some repo that returns json string >>
Then call
var products = JsonConvert.DeserializeObject<List<Product>>(productsJson);
NOTE: You will need to make your JSON valid first, it is syntactically incorrect at the moment
******EDIT*******
The question needs updating (see comments), your models would look like this
public class Self
{
public string DeviceID { get; set; }
public string TimeStamp { get; set; }
}
public class Location
{
public string RoomID { get; set; }
public string Room { get; set; }
}
public class Friend
{
public string IdUser { get; set; }
public string Name { get; set; }
public Location Location { get; set; }
}
public class RootObject
{
public Self Self { get; set; }
public List<Friend> Friends { get; set; }
}
then
var products = JsonConvert.DeserializeObject<RootObject>(productsJson);