How to create a treeview in c# when Deserialize Json to object? - c#

I'm new into c# and wpf. i have successful deserialize the json data into object by using restsharp.
Currently my json data returns when call a GET method has something looks like this:
{
"elements":[
{
"id":1,
"subject":"New website",
"_links":{
"children":[
{
"href":"/api/v3/work_packages/6",
"title":"New landing page"
},
{
"href":"/api/v3/work_packages/4",
"title":"Newsletter registration form"
},
{
"href":"/api/v3/work_packages/5",
"title":"Implement product tour"
}
]
}
},
{
"id":6,
"subject":"New landing page",
"_links":{
"ancestors":[
{
"href":"/api/v3/work_packages/3",
"title":"New website"
}
]
}
}
]
}
and more
So my question is that how to put it into a treeview template?
public class Outer
{
public List<RootObjectForAncestor> elements { get; set; }
public List<RootObjectForChild> elements { get; set; }
}
public class RootObjectForAncestor
{
public string id { get; set; }
public string subject { get; set; }
public LinksChild _links { get; set; }
}
public class LinksChild
{
public List<Child> children { get; set; }
}
public class Child
{
public string href { get; set; }
public string title { get; set; }
}
####and this below for the child class #####
public class RootObjectForChild
{
public int id { get; set; }
public string subject { get; set; }
public LinksAncestor _links { get; set; }
}
public class Ancestor
{
public string href { get; set; }
public string title { get; set; }
}
public class LinksAncestor
{
public List<Ancestor> ancestors { get; set; }
}
My function for getting normal get method looks like this
var client = new RestClient("my endpoint");
client.Authenticator = newHttpBasicAuthenticator("apikey","myapikey");
var request = new RestSharp.RestRequest("api/v3/projects/1/work_packages", Method.GET);
IRestResponse response = client.Execute(request);
var obj = JsonConvert.DeserializeObject<Outer>(response.Content);
So the question is how do i put these binding into the treeview?

Related

C# class representation for this json

I need little help to create C# class to map below json response from FCM token info api (https://developers.google.com/instance-id/reference/server#get_information_about_app_instances):
{
"application": "com.chrome.windows",
"subtype": "wp:www.mydomain.com/#A1249A346-7458-45BB-A0F2-2AC4856BB-V2",
"scope": "*",
"authorizedEntity": "8212312155",
"rel": {
"topics": {
"topic1": {
"addDate": "2020-12-06"
}
}
},
"platform": "BROWSER"
}
Where topic1 is not the property name, rather a value and topics which is a property name contains the list of topics.
I am actually not sure how to represent the topics in above json.
It is Dictionary format.
public Dictionary<string, Topic> Topics {get;set;}
The class is like below:
public class Root
{
public string application { get; set; }
public string subtype { get; set; }
public string scope { get; set; }
public string authorizedEntity { get; set; }
public Rel rel { get; set; }
public string platform { get; set; }
}
public class Rel
{
public Dictionary<string, Topic> topics { get; set; }
}
public class Topic
{
public string addDate { get; set; }
}
The above json can be serialized from the below model:
var root = new Root
{
application = "com.chrome.windows",
subtype = "wp:www.mydomain.com/#A1249A346-7458-45BB-A0F2-2AC4856BB-V2",
scope = "*",
authorizedEntity = "8212312155",
rel = new Rel
{
topics = new Dictionary<string, Topic>
{
{
"topic1",
new Topic
{
addDate = "2020-12-06"
}
}
}
},
platform = "BROWSER"
};
public class Topic1 {
public string addDate { get; set; }
}
public class Topics {
public Topic1 topic1 { get; set; }
}
public class Rel {
public Topics topics { get; set; }
}
public class Root {
public string application { get; set; }
public string subtype { get; set; }
public string scope { get; set; }
public string authorizedEntity { get; set; }
public Rel rel { get; set; }
public string platform { get; set; }
}
https://json2csharp.com/
If you are sure that topics contain unique topic names , so use Dictionary, if not sure, then List

How to bind this json and read specific key value

I have below json which I would like to read it in a list. below are the classes and model defined and the code by which I am trying to read. I am getting null value on binding. I am not sure how should i achieve this. So for example if I have multiple rules, I would like to read each one based on the condition passed. Please see my last sample code for better understanding.
Sample Json:
{"TableStorageRule": { "Rules": [ {
"Name": "filterRule1",
"DataFilter":
{
"DataSetType": "Settings1"
},
"TableSettings":
{
"AzureTable": {
"Account": "account1",
"Table": "table1",
"Key": "key1"
},
"SchemaBaseUri": "https://test.web.core.windows.net/"
}
},
{
"Name": "filterRule2",
"DataFilter":
{
"DataSetType": "Settings2"
},
"TableSettings":
{
"AzureTable": {
"Account": "account2",
"Table": "table2",
"Key": "key2"
},
"SchemaBaseUri": "https://test2.web.core.windows.net/"
}
}
] }}
Model and Code:
public class TableStoreSettings
{
public class AzureTableSettings
{
public string Account { get; set; }
public string Key { get; set; }
public string Table { get; set; }
}
public AzureTableSettings AzureTable { get; set; }
public Uri SchemaBaseUri { get; set; }
}
public class TableStorageRule
{
public string Name { get; set; }
public TwisterDataFilter DataFilter { get; set; }
public TableStoreSettings TableSettings { get; set; }
}
public class TableStorageConfiguration
{
public IEnumerable<TableStorageRule> Rules { get; set; } = new List<TableStorageRule>();
}
code by which I am trying to read:
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Root))
.AddJsonFile("appsettings.json", optional: false);
var config = builder.Build();
var tableStorageOutput = new TableStorageRule();
config.GetSection("TableStorageRule").Bind(tableStorageOutput);
var nameOfFilter = tableStorageOutput.Name;
if (tableStorageOutput.Name == "filterRule1")
{
var accountname = tableStorageOutput.TableSettings.AzureTable.Account;
}
on above I only get first filtername1 , i dont get other filternames and so on...though on GetSection() i see all the values in quick watch.
this is correct models from your JSON file :
public class DataFilter {
public string DataSetType { get; set; }
}
public class AzureTable {
public string Account { get; set; }
public string Table { get; set; }
public string Key { get; set; }
}
public class TableSettings {
public AzureTable AzureTable { get; set; }
public string SchemaBaseUri { get; set; }
}
public class Rule {
public string Name { get; set; }
public DataFilter DataFilter { get; set; }
public TableSettings TableSettings { get; set; }
}
public class TableStorageRule {
public List<Rule> Rules { get; set; }
}
public class Root {
public TableStorageRule TableStorageRule { get; set; }
}
for test you can read your JSON file and get values (maybe change models solve your problem in your code):
string json = File.ReadAllText(jsonFilePath);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);

How can i get auto generated id from JSON in reactJS or C#

how can i get auto generated id from this object in an array
thank you
{
"CourseList": {
"-Lti4CFDdJwAkBW5ujqm": { //auto generated id
"a1_courseName": "robotics",
"a2_courseCode": "1212"
},
"-LtrpJSDN4OCQAyJiSmQ": { //auto generated id
"a1_courseName": "shsh",
"a2_courseCode": "hhshs"
}
},
"PersonalInfo": {
"email": "arifulis#******.edu",
"teacherInstitute": "####",
"teacherName": "arif"
}
}
I have tested with NewtonSoft Json, generate it as Dictionary,
public partial class CourseAndnfo
{
public Dictionary<string, CourseList> CourseList { get; set; }
public PersonalInfo PersonalInfo { get; set; }
}
public partial class CourseList
{
public string a1_courseName { get; set; }
public string a2_courseCode { get; set; }
}
public partial class PersonalInfo
{
public string Email { get; set; }
public string TeacherInstitute { get; set; }
public string TeacherName { get; set; }
}
Deserialize it using,
var ParsedJson = JsonConvert.DeserializeObject<CourseAndInfo>(Yourjson);
List<string> AutoGeneratedIds = ParsedJson.CourseList.Keys.ToList();

Serializing Json to c# Class throwing error

I have a JSON returning from web like this
{
"data": {
"normal_customer": {
"0": {
"id": 1,
"name": "ALPHY"
}
},
"1": {
"id": 2,
"name": "STEVEN"
}
},
"luxury_customer": {
"3": {
"id": 8,
"name": "DEV"
}
}
}
}
I have created c# classes
public class StandardCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class CustomersDetails
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
public class LuxuryCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class Data
{
public StandardCustomers standard_Customers { get; set; }
public LuxuryCustomers luxury_Customers { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
}
When I use deserialize the response from the website using below c# code
var result1 = JsonConvert.DeserializeObject<Data>(response);
but result1.luxury_customers contains customerdetails which is null.
As suggested by #hellostone, I have modified to rootdata, then also
result1.luxury_customers contains customerdetails is null.
Any idea how to deserialize to c# class
When we pasted Json to visual studio, it generated classes as below
public class Rootobject
{
public Data data { get; set; }
}
public class Data
{
public Standard_Customers standard_Customers { get; set; }
public Luxury_Customers luxury_Customers { get; set; }
}
public class Standard_Customers
{
public _0 _0 { get; set; }
public _1 _1 { get; set; }
public _2 _2 { get; set; }
public _4 _4 { get; set; }
public _5 _5 { get; set; }
}
public class _0
{
public int id { get; set; }
public string name { get; set; }
}
individual classes are generated in standard customers , can we use list for this
I guess the problem is that index in luxury_customers starting not from zero. Try to use Dictionary<string,CustomersDetails> in LuxuryCustomers instead List<CustomersDetails>.
I've managed to deserialize Json with this classes:
public class CustomersDetails
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
}
public class Data
{
public Dictionary<string, CustomersDetails> normal_customer { get; set; }
public Dictionary<string,CustomersDetails> luxury_customer { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}
Deserialization code:
var result = JsonConvert.DeserializeObject<RootObject>(text);
P.S. I've remove one closing bracket after "ALPHY" element, to make Json valid, I hope it was typo and you're getting valid Json.
Your json string doesn't match with your defined classes. Your Json string should look like this if you want to preserve your class structure:
{
"data":{
"standard_Customers":{
"Customers_Details":[
{
"id":1,
"name":"ALPHY"
},
{
"id":2,
"name":"STEVEN"
}
]
},
"luxury_Customers":{
"Customers_Details":[
{
"id":8,
"name":"DEV"
}
]
}
}
}
Pay attention to the square brackets at the "Customers_Details" attribute.
Then the:
var result1 = JsonConvert.DeserializeObject<RootObject>(response);
call, will give you the right object back and it shouldn't be null, when using your class structure:
public class StandardCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class CustomersDetails
{
[JsonProperty("id")]
public int id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
}
public class LuxuryCustomers
{
public List<CustomersDetails> Customers_Details { get; set; }
}
public class Data
{
public StandardCustomers standard_Customers { get; set; }
public LuxuryCustomers luxury_Customers { get; set; }
}
public class RootObject
{
public Data data { get; set; }
}

deserializing JSON to .net object

I'm having problems deserializing JSON to objects. I read a couple of answers but none of them helped me.
This is my JSON file :
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "title",
"name": "name",
"url": "url"
}
}
}
And this is my class :
public class TextInfo
{
public class Meta
{
public int status { get; set; }
public string msg { get; set; }
}
public class Blog
{
public string title { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Response
{
public Blog blog { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
public Response response { get; set; }
}
}
Now, I've tried to deserialize the json like so, but i don't know how to manipulate the data :
TextInfo txt = JsonConvert.DeserializeObject<TextInfo>(json);
Can you help me figure this out ? Thanks in advance.
Three things:
can you provide an error message/log,
do you parse just one item or multiple items? In second case: var txt = JsonConvert.DeserializeObject<List<TextInfo>>(JSONstr);
Try the following:
public class Meta
{
public int status { get; set; }
public string msg { get; set; }
}
public class Response
{
public Blog blog { get; set; }
}
public class Blog
{
public string title { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class TextInfo
{
public Meta meta { get; set; }
public Response response { get; set; }
}
The only difference is that TextInfo should contain Meta and Response:
public class TextInfo
{
public Meta meta { get; set; }
public Response response { get; set; }
}
public class Meta
{
public int status { get; set; }
public string msg { get; set; }
}
public class Blog
{
public string title { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Response
{
public Blog blog { get; set; }
}
And you deserialize like you said:
TextInfo txt = JsonConvert.DeserializeObject<TextInfo>(json);
Your TextInfo class does not have any properties inside it to deserialize. It only declares another classes.
According to your source, you should deserialize into RootObject. This will return you an instance with meta and response properties.
Then you'll access them:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(obj.meta.msg);

Categories