Deserialise JSON containing numeric key. Json.NET [duplicate] - c#

This question already has answers here:
Deserialise JSON containing numeric key with Json.NET
(3 answers)
Closed 2 years ago.
I want to deserialize the JSON (using Json.NET) to an object. But can't, because the class name would need to begin with a number.
{
"status":"ok",
"meta":{
"count":1
},
"data":{
"7887207":{
"global_rating":9524,
"statistics":{
"random":{
"damage_dealt":54395747,
"wins":17502,
"hits_percents":74,
"battle_avg_xp":797,
"battles":31389
}
},
"last_battle_time":1600087522
}
}
}
How could I deserialize this JSON containing a number? I tried to use following guide (Deserialise JSON containing numeric key with Json.NET)
public class MetaGetPlayerStats
{
public int count { get; set; }
}
public class RandomGetPlayerStats
{
public int damage_dealt { get; set; }
public int wins { get; set; }
public int hits_percents { get; set; }
public int battle_avg_xp { get; set; }
public int battles { get; set; }
}
public class StatisticsGetPlayerStats
{
public RandomGetPlayerStats random { get; set; }
}
public class AccountGetPlayerStats
{
public int global_rating { get; set; }
public StatisticsGetPlayerStats statistics { get; set; }
public int last_battle_time { get; set; }
}
public class AccountInfo
{
[JsonProperty("data")]
public Dictionary<int, AccountGetPlayerStats> AccountInfoStats { get; set; }
}
public class GetPlayerStats
{
public string status { get; set; }
public MetaGetPlayerStats meta { get; set; }
public AccountInfo data { get; set; }
}
But it doesn't work. It saw fields "status" and "meta", but "data" == null Image

AccountInfo is excess. data is a dictionary. This will work.
public class GetPlayerStats
{
public string status { get; set; }
public MetaGetPlayerStats meta { get; set; }
public Dictionary<string, AccountGetPlayerStats> data { get; set; }
}

You just need to change your GetPlayerStats class like this:
public class GetPlayerStats
{
public string status { get; set; }
public MetaGetPlayerStats meta { get; set; }
[JsonProperty("data")]
public Dictionary<string, JObject> AccountInfoStats { get; set; }
// public AccountInfo data { get; set; }
}
then you can proceed to the logic, follow reference.
Deserialize JSON with "random" key

Related

Deserializing JSON into C# Class with dynamic object [duplicate]

This question already has answers here:
Complicated Json to C# Object Deserialize with classes
(2 answers)
Closed 7 months ago.
I have json being returned from an API. The JSON is formatted as below:
{
"success":true,
"code":200,
"total":2,
"data":{
"1019588":{
"name":"(t) Bob Jones",
"calls":213,
"user_id":"1019588"
},
"1019741":{
"name":"(t) Chris Smith",
"calls":387,
"user_id":"1019741"
}
}
}
I am trying to deserialize into a C# class but I am having issues with the dynamic id for each employee row.
My code:
AgentPeformanceResponse viewModel = JsonSerializer.Deserialize<AgentPeformanceResponse>(result.Result);
public class AgentPeformanceResponse
{
public bool success { get; set; }
public int code { get; set; }
public int total { get; set; }
public Data data { get; set; }
public AgentPeformanceResponse()
{
data = new Data();
}
}
public class Data
{
public Data()
{
PerformanceReponse = new List<PerformanceReponse>();
}
public List<PerformanceReponse> PerformanceReponse { get; set; }
}
public class PerformanceReponse
{
public string name { get; set; }
public int calls { get; set; }
public string user_id { get; set; }
}
How do I handle the dynamic employee ID so that I can deserialize it all into one object?
You should use a Dictionary:
public class AgentPeformanceResponse
{
public bool success { get; set; }
public int code { get; set; }
public int total { get; set; }
public Dictionary<string,PerformanceReponse> data { get; set; }
}
public class PerformanceReponse
{
public string name { get; set; }
public int calls { get; set; }
public string user_id { get; set; }
}
Example:
string json = #"{
""success"":true,
""code"":200,
""total"":2,
""data"":{
""1019588"":{
""name"":""(t) Bob Jones"",
""calls"":213,
""user_id"":""1019588""
},
""1019741"":{
""name"":""(t) Chris Smith"",
""calls"":387,
""user_id"":""1019741""
}
}
}";
var obj = System.Text.Json.JsonSerializer
.Deserialize<AgentPeformanceResponse>(json);
using System;
using System.Collections.Generic;
public class AgentPeformanceResponse
{
public bool success { get; set; }
public int code { get; set; }
public int total { get; set; }
public Dictionary<string, PerformanceReponse> data { get; set; }
}
public class PerformanceReponse
{
public string name { get; set; }
public int calls { get; set; }
public string user_id { get; set; }
}
public class Program
{
public static void Main()
{
var json = "{\"success\":true,\"code\":200,\"total\":2,\"data\":{\"1019588\":{\"name\":\"(t) Bob Jones\",\"calls\":213,\"user_id\":\"1019588\"},\"1019741\":{\"name\":\"(t) Chris Smith\",\"calls\":387,\"user_id\":\"1019741\"}}}";
var result = System.Text.Json.JsonSerializer.Deserialize<AgentPeformanceResponse>(json);
Console.WriteLine(result.code);
Console.WriteLine(result.data["1019741"].name);
}
}
The output will be
200
(t) Chris Smith
Fiddle for you
https://dotnetfiddle.net/lQu2Ln
all code you really need
Dictionary<string, PerformanceReponse> dict = JsonDocument.Parse(json).RootElement
.GetProperty("data").Deserialize<Dictionary<string,PerformanceReponse>>();
//or if you want a list
List<PerformanceReponse> list = data.Select(d=>d.Value).ToList();
or using Newtonsoft.Json
Dictionary<string,PerformanceReponse> dict = JObject.Parse(json)
["data"].ToObject<Dictionary<string,PerformanceReponse>>();
how to use
PerformanceReponse data1019588= dict["1019588"];

JSON DeserializeObject to Model without Key Name

I currently have JSON coming in as follows:
{"36879":[{"min_qty":1,"discount_type":"%","csp_price":10}],"57950":[{"min_qty":1,"discount_type":"flat","csp_price":650}]}
This contains a list of the following records
ProductId
MinQty
DiscountType
Price
I need to deserialize this into the following model:
public class CustomerSpecificPricing
{
string productId { get; set; }
public virtual List<CustomerSpecificPricingDetail> CustomerSpecificPricingDetails { get; set; }
}
public class CustomerSpecificPricingDetail
{
public string min_qty { get; set; }
public string discount_type { get; set; }
public string csp_price { get; set; }
}
The problem is that the "productId" of each record is missing the key name.
If I run my JSON through J2C, I get the following:
public class 36879 {
public int min_qty { get; set; }
public string discount_type { get; set; }
public int csp_price { get; set; }
}
public class 57950 {
public int min_qty { get; set; }
public string discount_type { get; set; }
public int csp_price { get; set; }
}
public class Root {
public List<_36879> _36879 { get; set; }
public List<_57950> _57950 { get; set; }
}
Which is obviously incorrect.
How would I deserialize my object correctly?
You would need to deserialize it into a dictionary first and then map it into the format you require after. Something like this should work:
var dict = JsonConvert.DeserializeObject<Dictionary<string, IEnumerable<CustomerSpecificPricingDetail>>>();
var result = dict.Select(kvp => new CustomerSpecificPricing { ProductId = Int32.Parse(kvp.Key), CustomerSpecificPricingDetails = kvp.Value });
Id also recommend you follow the conventional standards of naming. In this case properties in classes should be PascalCase,
e.g. your classes now become:
public class CustomerSpecificPricing
{
[JsonProperty("productId ")]
public string ProductId { get; set; }
public virtual List<CustomerSpecificPricingDetail> CustomerSpecificPricingDetails { get; set; }
}
and
public class CustomerSpecificPricingDetail
{
[JsonProperty("min_qty")]
public string MinQty { get; set; }
[JsonProperty("discount_type ")]
public string DiscountType { get; set; }
[JsonProperty("csp_price ")]
public string CspPrice { get; set; }
}

c# json with a changing class

public class 2500113075262000 {
public string pair { get; set; }
public string type { get; set; }
public double amount { get; set; }
public int rate { get; set; }
public string timestamp_created { get; set; }
public int status { get; set; }
}
public class Return {
public 2500113075262000 2500113075262000 { get; set; }
}
public class Root {
public int success { get; set; }
public Return #return { get; set; }
}
class 2500113075262000 is constantly changing, this is the order ID, like deserialize
{"success":1,"return":{"2500113075262000":{"pair":"eth_rur","type":"sell","amount":0.00110569,"rate":46100,"timestamp_created":"1608918997","status":0}}}
It looks like it's only the key - presumably the order ID - which is changing. I would suggest removing your Return class entirely, and changing your Root class to have a Dictionary<string, Order>. I'd also suggest writing your classes with idiomatic .NET property names, and using JsonPropertyAttribute to specify the representation in the JSON. So it would be something like this:
public class Order
{
[JsonProperty("pair")]
public string Pair { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
// etc, other properties
}
public class Root
{
[JsonProperty("success")]
public int Success { get; set; }
[JsonProperty("return")]
public Dictionary<string, Order> Returns { get; set; }
}

C# Parsing JSON String

I have tried countless methods to Parse my JSON string (Steam Public Data), yet nothing seems to work. I just want to be able to extract values from the string. For Example, obtaining the value of personaname which would return SlothGod. I have JSON.NET installed in my project.
Here is my JSON:
{
"response": {
"players": [
{
"steamid": "76561198301407459",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "SlothGod",
"lastlogoff": 1508389707,
"commentpermission": 1,
"profileurl": "http://steamcommunity.com/id/sleuthgud/",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba.jpg",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba_medium.jpg",
"avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba_full.jpg",
"personastate": 0,
"realname": "Josh",
"primaryclanid": "103582791460168790",
"timecreated": 1462086929,
"personastateflags": 0,
"loccountrycode": "AU",
"locstatecode": "QLD"
}
]
}
}
Main method suggested to me:
public class Details
{
public string personaname { get; set; }
}
private void GetSteamDetails()
{
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Details>(SteamDetailsJson);
SteamName = data.personaname;
}
This is placed before Page_Load(). I then call GetSteamDetails(); when I want to fetch the name.
After my question being down voted, I decided to not give up on this problem. After extensive research, trial and error, and YouTube tutorials which are the most helpful IMO. I found that the data was containing a JSON array, and yes I will admit, I was confused with this, but the answer was to simply treat it like a C# array and add [1] to the end of players.
Details details = new Details();
public class Details
{
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public string realname { get; set; }
public string personaname { get; set; }
public string steamid { get; set; }
}
private void GetSteamDetails()
{
var SteamDetails= JsonConvert.DeserializeObject<dynamic>(SteamDetailsJson);
avatar = SteamDetails.response.players[1].avatar.ToString();
personaname = SteamDetails.response.players[1].personaname.ToString();
}
Based on the JSON string you provided, you should have the following C# classes to support it, or to deserialize the JSON object values into: I used this link to generate the classes.
public class Player
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public int lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public int timecreated { get; set; }
public int personastateflags { get; set; }
public string loccountrycode { get; set; }
public string locstatecode { get; set; }
}
public class Response
{
public List<Player> players { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
Then, using Newtonsoft.Json, you can deserialize the JSON object into your C# classes as follow:
JsonConvert.DeserializeObject<RootObject>("yourJsonString");
You mention that Newtonsoft.Json already referenced in the project.
Use class to represent json data structure, then you can easy deserialize it.
You can use only properties you need in the class.
public class Player
{
public string personaname { get; set; }
}
var player = Newtonsoft.Json.JsonConvert.DeserializeObject<Player>(jsonString);
// use player.personaname
For updates question create classes which represent your data structure
public class Team
{
public List<Player> players { get; set; }
}
public class Response
{
public Team response { get; set; }
}
You can use http://json2csharp.com/ to generate a class automatically from a JSON string.

Json not mapping properly in C#

Here is a json document that I wanted to map it to C# poco classes. I wrote some classes but they didn't work. I got null in my result object. Any ideas?
I used Newtonsoft's json converter.
{
"retrieval-response":{
"cdata":{
"identifier":"777400",
"document-count":"62"
},
"index":"10",
"count":"25"
}
}
C# map classes;
public class result
{
[JsonProperty("retrieval-response")]
public aResult res { get; set; }
public int index { get; set; }
public int count { get; set; }
}
public class aResult
{
public cdata data { get; set; }
}
public class cdata
{
[JsonProperty("identifier")]
public string identif { get; set; }
[JsonProperty("document-count")]
public string count { get; set; }
}
You model is wrong. Try this:
public class Wrapper
{
[JsonProperty("retrieval-response")]
public Result Result { get; set; }
}
public class Result
{
[JsonProperty("cdata")]
public Data Data { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
public class Data
{
[JsonProperty("identifier")]
public string Identifier { get; set; }
[JsonProperty("document-count")]
public string Count { get; set; }
}
Then you can deserialize it with the following line:
var myResult = JsonConvert.DeserializeObject<Wrapper>(json);
Please note that I've also wrote your property and class names in pascal case. These are the naming conventions from Microsoft.

Categories