Create a strongly typed c# object from json object with ID as the name
Make your root object be a dictionary:
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, SessionPerformanceStats>>(theJsonResponse);
Json.NET serializes a dictionary from and to a JSON object, with the keys being converted to the property names. In your case, the ID numbers will be deserialized as the dictionary keys. If you are sure they will always be numbers, you could declare them as such:
var dictionary = JsonConvert.DeserializeObject<Dictionary<long, SessionPerformanceStats>>(theJsonResponse);
See Serialize a Dictionary and Deserialize a Dictionary
create POCO when id property in json acts like object
I would define some "Event" class like this with
name
start
end
detail
image_url
Then, I would try to deserialize events
as a Dictionary<int, Event>
.
After that, you can always map this to another EventWithId class similar to the Event class with an additional id field, using some LINQ query on the resulting Dictionary.
JSON to C# Class Invalid Type Object with Inconsistent Numerical Names
You're 99.99% there. Use the JsonProperty attribute to tell it that UserProperties
is called user
in the JSON, and JsonConvert.DeserializeObject<User>(USER)
returns a valid User
.
[JsonObject(Description = "user")]
public class User
{
[JsonProperty(PropertyName = "user")]
public Dictionary<string, UserProperties> UserPropererties { get; set; }
}
You could also just rename User.UserProperties to User.user
, but I assume you'd rather have some control over the process.
You could make that Dictionary<int, UserProperties>
, too. Works for me.
How to convert JSON to Objects with changing Keys
You can use below class structure to deserialize your json.
class Rooot
{
public Dictionary<string, BankDetail> Result { get; set; }
}
public class BankDetail
{
public string BANK { get; set; }
public string IFSC { get; set; }
public string BRANCH { get; set; }
public string ADDRESS { get; set; }
public object CONTACT { get; set; }
public string CITY { get; set; }
public string DISTRICT { get; set; }
public string STATE { get; set; }
}
Usage:
string json = File.ReadAllText(IFSCCodeFile);
Rooot rooot = JsonConvert.DeserializeObject<Rooot>(json);
foreach (var item in rooot.Result)
{
Console.WriteLine(item.Key);
Console.WriteLine(item.Value.BANK ?? "NULL");
Console.WriteLine(item.Value.IFSC ?? "NULL");
Console.WriteLine(item.Value.BRANCH ?? "NULL");
Console.WriteLine(item.Value.ADDRESS ?? "NULL");
Console.WriteLine(item.Value.CONTACT ?? "NULL");
Console.WriteLine(item.Value.CITY ?? "NULL");
Console.WriteLine(item.Value.DISTRICT ?? "NULL");
Console.WriteLine(item.Value.STATE ?? "NULL");
Console.WriteLine();
}
Console.ReadLine();
Output:
Deserialize JSON Object to C# class after API call
Your biggest issue is that your model doesn't match your json string. You can use a dictionary to represent your data and your model would look something like this:
public void Deserialize()
{
var obj = JsonConvert.DeserializeObject<RootObject>(json);
}
private string json = @"{""status"": ""ok"",
""meta"": {
""count"": 1,
""hidden"": null
},
""data"": {
""111111111"": [
{
""ship_id"": 4180588496
},
{
""ship_id"": 4284430032
},
{
""ship_id"": 3767482320
}
]
}
}";
public class RootObject
{
public string Status { get; set; }
public MetaData Meta { get; set; }
public IDictionary<string, IEnumerable<Ship>> Data { get; set; }
}
public class MetaData
{
public int Count { get; set; }
public string Hidden { get; set; }
}
public class Ship
{
public long Ship_Id { get; set; }
}
Related Topics
How to Remove Elements from a Generic List While Iterating Over It
Process.Start: How to Get the Output
What Does the =≫ Operator Mean in a Property
Identify If a String Is a Number
Listing All Permutations of a String/Integer
In C#, Difference Between Public, Private, Protected, and Having No Access Modifier
Protect .Net Code from Reverse Engineering
Difference Between Task and Thread
Using Cookiecontainer With Webclient Class
The Located Assembly'S Manifest Definition Does Not Match the Assembly Reference
Dependency Inject (Di) "Friendly" Library
Best Way to Randomize an Array With .Net
Type Checking: Typeof, Gettype, or Is
How to Remove All Event Handlers from an Event
Why Covariance and Contravariance Do Not Support Value Type
Best Way to Keep Track and Iterate Through Tabs and Windows Using Windowhandles Using Selenium