extract data from json in asp.net c# - c#

I'm trying to extract some data from json. I've been looking for a solution either in JSS or Json.net but haven't been able to figure this problem out. this is how my Json looks like:
Note: i Have tested and the mapping and decentralization works! I'm looking for a way to extract specifc data from the json!
Thanks in Advance!
{
"tasks":[
{
"id":"tmp_fk1345624806538",
"name":"Gantt editor ",
"code":"",
"level":0,
"status":"STATUS_ACTIVE",
"start":1346623200000,
"duration":5,
"end":1347055199999,
"startIsMilestone":false,
"endIsMilestone":false,
"assigs":[
{
"resourceId":"tmp_3",
"id":"tmp_1345625008213",
"roleId":"tmp_1",
"effort":7200000
}
],
"depends":"",
"description":"",
"progress":0
},
{
"id":"tmp_fk1345624806539",
"name":"phase 1",
"code":"",
"level":1,
"status":"STATUS_ACTIVE",
"start":1346623200000,
"duration":2,
"end":1346795999999,
"startIsMilestone":false,
"endIsMilestone":false,
"assigs":[
{
"resourceId":"tmp_1",
"id":"tmp_1345624980735",
"roleId":"tmp_1",
"effort":36000000
}
],
"depends":"",
"description":"",
"progress":0
},
{
"id":"tmp_fk1345624789530",
"name":"phase 2",
"code":"",
"level":1,
"status":"STATUS_SUSPENDED",
"start":1346796000000,
"duration":3,
"end":1347055199999,
"startIsMilestone":false,
"endIsMilestone":false,
"assigs":[
{
"resourceId":"tmp_2",
"id":"tmp_1345624993405",
"roleId":"tmp_2",
"effort":36000000
}
],
"depends":"2",
"description":"",
"progress":0
}
],
"resources":[
{
"id":"tmp_1",
"name":"Resource 1"
},
{
"id":"tmp_2",
"name":"Resource 2"
},
{
"id":"tmp_3",
"name":"Resource 3"
}
],"roles":[
{
"id":"tmp_1",
"name":"Project Manager"
},
{
"id":"tmp_2",
"name":"Worker"
}
],
"canWrite":true,
"canWriteOnParent":true,
"selectedRow":0,
"deletedTaskIds":[],
}
i've already mapped as follow
public class Rootobject
{
public Task[] tasks { get; set; }
public Resource[] resources { get; set; }
public Role[] roles { get; set; }
public bool canWrite { get; set; }
public bool canWriteOnParent { get; set; }
public int selectedRow { get; set; }
public object[] deletedTaskIds { get; set; }
}
public class Task
{
public string id { get; set; }
public string name { get; set; }
public string code { get; set; }
public int level { get; set; }
public string status { get; set; }
public long start { get; set; }
public int duration { get; set; }
public long end { get; set; }
public bool startIsMilestone { get; set; }
public bool endIsMilestone { get; set; }
public Assig[] assigs { get; set; }
public string depends { get; set; }
public string description { get; set; }
public int progress { get; set; }
}
public class Assig
{
public string resourceId { get; set; }
public string id { get; set; }
public string roleId { get; set; }
public int effort { get; set; }
}
public class Resource
{
public string id { get; set; }
public string name { get; set; }
}
public class Role
{
public string id { get; set; }
public string name { get; set; }
}
and I need to extract following information from my json.(from specific Task in may json! for example the first one with id : tmp_fk1345624806538 ).
Note: i'm getting my json from a json file as follow:
string startDate; // this is what i need to extract
string endDate; // this is what i need to extract
string Progress; // this is what i need to extract
public void load()
{
GC.GClass l = new GC.GClass();
string jsonString = l.load(); // i get my json from a json file
Rootobject project = JsonConvert.DeserializeObject<Rootobject>(jsonString);
}

You can use LINQ to query the object quickly.
Task task = project.tasks.FirstOrDefault(t=> t.id == "tmp_fk1345624806538");
Test task, and if null then there was not task with matching id. If you are sure that there will be a matching task your can just use .First(), but it will throw an exception if there is no match in the list
You'll need to add a using System.Linq; if you don't have that already.

Related

How do I compare two JSON Objects to get new JSON for changes like add,delete,change using c#

Can anyone help in getting the output Json by comparing two JSON which has same hierarchy, by showing JStatus as Change, Added, Delete. we have JStatus present in each Json, after comparison it will fill with status "change"/"add"/"delete", the values should be from new (2nd json) for changed objects, and value of old (in case of delete) json1.
please help
1st Base JSON (output should treat this as base JSON)
[{"Decision":{"id":"1","identifier":"Base1","dec_id":10,"JStatus":"","objects":[{"id":"1","identifier":"Base2","JStatus":""},{"id":"2","identifier":"Base3","JStatus":""}]}}]
2nd JSON
[{"Decision":{"id":"2","identifier":"Base1","dec_id":12,"JStatus":"","objects":[{"id":"1","identifier":"Base2","JStatus":"","textitem":[{"id":"1","identifier":"Base3","JStatus":"","objNewActivity":[{"id":"1","identifier":"Base4","JStatus":""}],"objNewGebiedsaanwijzingen":[{"id":"1","identifier":"Base5","JStatus":""}],"objNewBegrippen":[{"id":"1","identifier":"Base6","JStatus":""}],"objNewLocation":null}]}]}}]
OutPut required JSON
[{"Decision":{"id":"2","identifier":"Base1","dec_id":12,"JStatus":"Change","objects":[{"id":"1","identifier":"Base2","JStatus":"","textitem":[{"id":"1","identifier":"Base3","JStatus":"Add","objNewActivity":[{"id":"1","identifier":"Base4","JStatus":"Add"}],"objNewGebiedsaanwijzingen":[{"id":"1","identifier":"Base5","JStatus":"Add"}],"objNewBegrippen":[{"id":"1","identifier":"Base6","JStatus":"Add"}],"objNewLocation":null}]},{"id":"2","identifier":"Base3","JStatus":"Delete"}]}}]
I tried https://www.nuget.org/packages/JsonDiffer following but that only show me changes,
var j1 = JToken.Parse(readJson("Json1.txt"));
var j2 = JToken.Parse(readJson("Json2.txt"));
var diff = JsonDifferentiator.Differentiate(j1, j2, OutputMode.Detailed, showOriginalValues: true);
This is PSEUDO CODE
Because I do not know the specific logic you wish to employ to make your comparisons and/or overwrites.
using Newtonsoft.Json;
using System.Collections.Generic;
namespace StackDemo5
{
public class StackDemo5Main
{
string json1 = "{ \"LoginSettings\": { \"Type\": \"Test\", \"Login\": \"Bitcoin\", \"Password\": \"123456\", \"SHA1Login\": \"Some hash\", \"SHA1Password\": \"Some hash\" }, \"Info1\": {\"Is_Active\": false, \"Amount\": 12 }, \"Info2\": { \"Is_Active\": true, \"Amount\": 41 }}";
string json2 = "{ \"LoginSettings\": { \"Type\": \"\", \"Login\": \"\", \"Password\": \"\", \"SHA1Login\": \"\", \"SHA1Password\": \"\" }, \"Info1\": { \"Is_Active\": false, \"Amount\": 0 }, \"Info2\": { \"Is_Active\": false, \"Amount\": 0 }, \"Info3\": { \"Is_Active\": false, \"Amount\": 0 }, \"Info4\": { \"Is_Active\": false, \"Amount\": 0 }}";
public string CreateJsonAggregate() {
Root first = JsonConvert.DeserializeObject<Root>(json1);
Root second = JsonConvert.DeserializeObject<Root>(json2);
Root third = new Root();
third.LoginSettings = first.LoginSettings;
third.Info1 = first.Info1;
third.Info2 = first.Info2;
third.Info3 = second.Info3;
third.Info4 = second.Info4;
return JsonConvert.SerializeObject(third);
}
public class LoginSettings
{
public string Type { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string SHA1Login { get; set; }
public string SHA1Password { get; set; }
}
public class Info1
{
public bool Is_Active { get; set; }
public int Amount { get; set; }
}
public class Info2
{
public bool Is_Active { get; set; }
public int Amount { get; set; }
}
public class Info3
{
public bool Is_Active { get; set; }
public int Amount { get; set; }
}
public class Info4
{
public bool Is_Active { get; set; }
public int Amount { get; set; }
}
public class Root
{
public LoginSettings LoginSettings { get; set; }
public Info1 Info1 { get; set; }
public Info2 Info2 { get; set; }
public Info3 Info3 { get; set; }
public Info4 Info4 { get; set; }
}
}
public class StackDemo6 {
public string CreateJsonAggregate()
{
string input1 = "[{\"Decision\":{\"id\":\"1\",\"identifier\":\"Base1\",\"dec_id\":10,\"JStatus\":\"\",\"objects\":[{\"id\":\"1\",\"identifier\":\"Base2\",\"JStatus\":\"\"},{\"id\":\"2\",\"identifier\":\"Base3\",\"JStatus\":\"\"}]}}]";
string input2 = "[{\"Decision\":{\"id\":\"2\",\"identifier\":\"Base1\",\"dec_id\":12,\"JStatus\":\"\",\"objects\":[{\"id\":\"1\",\"identifier\":\"Base2\",\"JStatus\":\"\",\"textitem\":[{\"id\":\"1\",\"identifier\":\"Base3\",\"JStatus\":\"\",\"objNewActivity\":[{\"id\":\"1\",\"identifier\":\"Base4\",\"JStatus\":\"\"}],\"objNewGebiedsaanwijzingen\":[{\"id\":\"1\",\"identifier\":\"Base5\",\"JStatus\":\"\"}],\"objNewBegrippen\":[{\"id\":\"1\",\"identifier\":\"Base6\",\"JStatus\":\"\"}],\"objNewLocation\":null}]}]}}]";
Root input1Deserialized = JsonConvert.DeserializeObject<Root>(input1);
Root input2Deserialized = JsonConvert.DeserializeObject<Root>(input2);
Root resultObject = new Root();
// Build you comparison logic:
int dec_id;
if (input1Deserialized.Decision.dec_id > input2Deserialized.Decision.dec_id)
{
dec_id = input1Deserialized.Decision.dec_id;
}
else
{
dec_id = input2Deserialized.Decision.dec_id;
}
resultObject.Decision.dec_id = dec_id;
//etc.
return JsonConvert.SerializeObject(result);
}
}
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class ObjNewActivity
{
public string id { get; set; }
public string identifier { get; set; }
public string JStatus { get; set; }
}
public class ObjNewGebiedsaanwijzingen
{
public string id { get; set; }
public string identifier { get; set; }
public string JStatus { get; set; }
}
public class ObjNewBegrippen
{
public string id { get; set; }
public string identifier { get; set; }
public string JStatus { get; set; }
}
public class Textitem
{
public string id { get; set; }
public string identifier { get; set; }
public string JStatus { get; set; }
public List<ObjNewActivity> objNewActivity { get; set; }
public List<ObjNewGebiedsaanwijzingen> objNewGebiedsaanwijzingen { get; set; }
public List<ObjNewBegrippen> objNewBegrippen { get; set; }
public object objNewLocation { get; set; }
}
public class Object
{
public string id { get; set; }
public string identifier { get; set; }
public string JStatus { get; set; }
public List<Textitem> textitem { get; set; }
}
public class Decision
{
public string id { get; set; }
public string identifier { get; set; }
public int dec_id { get; set; }
public string JStatus { get; set; }
public List<Object> objects { get; set; }
}
public class Root
{
public Decision Decision { get; set; }
}
}
The trick is to create a generic model that can hold the most possible data, then serialize an object set from all your input strings, then do some sort of logic comparison, here I just did a comparison on dec_id, because I have no idea what your model does, and I don't speak dutch.
Also do write unit tests to validate your logic, it helps.
If you need to generate a new model object in C#, simply use this website from your template, to get a new class structure:
https://json2csharp.com/

Invalid type when generate the json?

I've this json:
{
"page": "36",
"bookmaker_urls": {
"13": [{
"link": "http://www.bet365.com/home/?affiliate=365_179024",
"name": "Bet 365"
}]
},
"block_service_id": "competition_summary_block_competitionmatchessummary",
"round_id": "36003",
"outgroup": "",
"view": "1",
"competition_id": "13"
}
I've inserted this on this tool: http://json2csharp.com/
this will return:
public class __invalid_type__13
{
public string link { get; set; }
public string name { get; set; }
}
public class BookmakerUrls
{
public List<__invalid_type__13> __invalid_name__13 { get; set; }
}
public class RootObject
{
public int page { get; set; }
public BookmakerUrls bookmaker_urls { get; set; }
public string block_service_id { get; set; }
public int round_id { get; set; }
public bool outgroup { get; set; }
public int view { get; set; }
public int competition_id { get; set; }
}
why there is an invalid type?
13 is not a valid property name in .NET, and the tool you're using seems to try to map each JSON property to .NET Class property.
What you probably want is bookmaker_urls to be a dictionary:
public class BookmakerUrl
{
public string link { get; set; }
public string name { get; set; }
}
public class RootObject
{
public int page { get; set; }
public Dictionary<string, List<BookmakerUrl>> bookmaker_urls { get; set; }
public string block_service_id { get; set; }
public int round_id { get; set; }
public bool outgroup { get; set; }
public int view { get; set; }
public int competition_id { get; set; }
}
The generator generates the name of properties and types from the name of properties in the Json.
There is a property "13" in the Json. A name starting with a digit would not be a valid name in c#.
So, the generator just adds the prefix "invalid_name" or "invalid_type" to the generated names. This does not mean that there was any problem or that you cannot use the generated code.

Deserialized JSON String to C# Object gives the error "Cannot access static class in non-static context" when the object's methods are called

I'm trying to GET a JSON REST-api request, deserialize it using the Newtonsoft.Json package for .NET and access methods within the newly created object, but I keep getting an error that won't let me run my C# code in Visual Studio 2015.
For the following JSON string,
{
"pagination":
{
"per_page": 1,
"items": 28,
"page": 1,
"urls":
{
"last": "https://...",
"next": "https://..."
},
"pages": 28
},
"results":
[{
"style": ["House"],
"thumb": "https://...",
"format": ["File", "AAC", "Album"],
"country": "Unknown",
"barcode": ["id886037928"],
"uri": "/Porter-Robinson-Worlds/master/721049",
"community": {"have": 932, "want": 720},
"label": ["Astralwerks", "Sample Sized, LLC", "Astralwerks"],
"catno": "none",
"year": "2014",
"genre": ["Electronic"],
"title": "Porter Robinson - Worlds",
"resource_url": "https://...",
"type": "master",
"id": 721049
}]
}
I created the following C# object class:
public class Discogs
{
public class pagination
{
public int per_page { get; set; }
public int items { get; set; }
public int page { get; set; }
public class urls
{
public string last { get; set; }
public string next { get; set; }
}
public int pages { get; set; }
public class data
{
public string[] style { get; set; }
public string thumb { get; set; }
public string[] format { get; set; }
public string country { get; set; }
public string[] barcode { get; set; }
public string uri { get; set; }
public class community
{
public string have { get; set; }
public string want { get; set; }
}
public string[] label { get; set; }
public string catno { get; set; }
public string year { get; set; }
public string[] genre { get; set; }
public string title { get; set; }
public string resource_url { get; set; }
public string type { get; set; }
public string id { get; set; }
}
public class results
{
public data Results { get; set; }
}
}
}
In a private async void class, I've successfully fetched the GET request and stored it in the string, jsonstring. Now I try to run this code:
Discogs myUser = new Discogs();
myUser = JsonConvert.DeserializeObject<Discogs>(jsonstring);
int yr = myUser.pagination.data.year;
...but my project gets an error, An object reference is required for the non-static field, method, or property 'Discogs.pagination.data.year', cannot access non-static property 'year' in static context.
This does not make sense to me because I have no static classes or methods. I've searched for a solution but all similar problems seem to be able to access deserialized objects without any such error. Any help on accessing the methods in my Discogs object would be greatly appreciated.
The problem is that you are trying to use the class Pagination without instantiating the class. In order to use a non-static class (Pagination, Data, Community) you must first instantiate them like below
Pagination pag = new Pagination();
Your structure here is pretty odd. Normally classes would be in separate files or at least not nested such as you have here. You may want to rethink the way you've designed this program.
You are trying to get value from "myUser.pagination...", but in your example "pagination" is a class name not a property inside "Discogs" class, same as "data" inside "pagination" class
code with nested classes:
public class Discogs
{
public class Pagination
{
public int per_page { get; set; }
public int items { get; set; }
public int page { get; set; }
public class Urls
{
public string last { get; set; }
public string next { get; set; }
}
public Urls urls {get;set;}
public int pages { get; set; }
public class Data
{
public string[] style { get; set; }
public string thumb { get; set; }
public string[] format { get; set; }
public string country { get; set; }
public string[] barcode { get; set; }
public string uri { get; set; }
public class Community
{
public string have { get; set; }
public string want { get; set; }
}
public Community community { get; set; }
public string[] label { get; set; }
public string catno { get; set; }
public string year { get; set; }
public string[] genre { get; set; }
public string title { get; set; }
public string resource_url { get; set; }
public string type { get; set; }
public string id { get; set; }
}
public class Results
{
public Data Results { get; set; }
}
public Results result {get;set;}
}
public Pagination pagination {get;set}
}
code with i think a bit easy to understand:
public class Urls
{
public string last { get; set; }
public string next { get; set; }
}
public class Community
{
public string have { get; set; }
public string want { get; set; }
}
public class Data
{
public string[] style { get; set; }
public string thumb { get; set; }
public string[] format { get; set; }
public string country { get; set; }
public string[] barcode { get; set; }
public string uri { get; set; }
public string[] label { get; set; }
public string catno { get; set; }
public string year { get; set; }
public string[] genre { get; set; }
public string title { get; set; }
public string resource_url { get; set; }
public string type { get; set; }
public string id { get; set; }
public Community community { get; set; }
}
public class Results
{
public Data Results { get; set; }
}
public class Pagination
{
public int per_page { get; set; }
public int items { get; set; }
public int page { get; set; }
public int pages { get; set; }
public Urls urls {get;set;}
public Results result {get;set;}
}
public class Discogs
{
public Pagination pagination {get;set}
}

Generate C# object from Json string and parse Json string to generated object

I am trying to generate C# class using the JSON string from here http://json2csharp.com/ this works fine. But I can't parse the JSON to the object generated by the website.
Here is the JSON string
{
"searchParameters":{
"key":"**********",
"system":"urn:oid:.8"
},
"message":" found one Person matching your search criteria.",
"_links":{
"self":{
"href":"https://integration.rest.api.test.com/v1/person?key=123456&system=12.4.34.."
}
},
"_embedded":{
"person":[
{
"details":{
"address":[
{
"line":["5554519 testdr"],
"city":"testland",
"state":"TT",
"zip":"12345",
"period":{
"start":"2003-10-22T00:00:00Z",
"end":"9999-12-31T23:59:59Z"
}
}
],
"name":[
{
"use":"usual",
"family":["BC"],
"given":["TWO"],
"period":{
"start":"9999-10-22T00:00:00Z",
"end":"9999-12-31T23:59:59Z"
}
}
],
"gender":{
"code":"M",
"display":"Male"
},
"birthDate":"9999-02-03T00:00:00Z",
"identifier":[
{
"use":"unspecified",
"system":"urn:oid:2.19.8",
"key":"",
"period":{
"start":"9999-10-22T00:00:00Z",
"end":"9999-12-31T23:59:59Z"
}
}
],
"telecom":[
{
"system":"email",
"value":"test#test.com",
"use":"unspecified",
"period":{
"start":"9999-10-22T00:00:00Z",
"end":"9999-12-31T23:59:59Z"
}
}
],
"photo":[
{
"content":{
"contentType":"image/jpeg",
"language":"",
"data":"",
"size":0,
"hash":"",
"title":"My Picture"
}
}
]
},
"enrolled":true,
"enrollmentSummary":{
"dateEnrolled":"9999-02-07T21:39:11.174Z",
"enroller":"test Support"
},
"_links":{
"self":{
"href":"https://integration.rest.api.test.com/v1/person/-182d-4296-90cc"
},
"unenroll":{
"href":"https://integration.rest.api.test.com/v1/person/1b018dc4-182d-4296-90cc-/unenroll"
},
"personLink":{
"href":"https://integration.rest.api.test.com/v1/person/-182d-4296-90cc-953c/personLink"
},
"personMatch":{
"href":"https://integration.rest.api.commonwellalliance.org/v1/person/-182d-4296-90cc-/personMatch?orgId="
}
}
}
]
}
}
Here is the code I use to convert to the object.
JavaScriptSerializer js = new JavaScriptSerializer();
var xx = (PersonsearchVM)js.Deserialize(jsonstr, typeof(PersonsearchVM));
Is there any other wat to generate the object and parse?
I think you have some invalid characters in your JSON string. Run it through a validator and add the necessary escape characters.
http://jsonlint.com/
You aren't casting it to the right object. Cast it to the type RootObject.
Eg
JavaScriptSerializer js = new JavaScriptSerializer();
var xx = (RootObject)js.Deserialize(jsonstr, typeof(RootObject));
The code that json 2 csharp creates is this:
public class SearchParameters
{
public string key { get; set; }
public string system { get; set; }
}
public class Self
{
public string href { get; set; }
}
public class LinKs
{
public Self self { get; set; }
}
public class Period
{
public string start { get; set; }
public string end { get; set; }
}
public class Address
{
public List<string> line { get; set; }
public string city { get; set; }
public string __invalid_name__state { get; set; }
public string zip { get; set; }
public Period period { get; set; }
}
public class PerioD2
{
public string start { get; set; }
public string end { get; set; }
}
public class Name
{
public string use { get; set; }
public List<string> family { get; set; }
public List<string> given { get; set; }
public PerioD2 __invalid_name__perio
d { get; set; }
}
public class Gender
{
public string __invalid_name__co
de { get; set; }
public string display { get; set; }
}
public class Period3
{
public string start { get; set; }
public string __invalid_name__end { get; set; }
}
public class Identifier
{
public string use
{ get; set; }
public string system { get; set; }
public string key { get; set; }
public Period3 period { get; set; }
}
public class Period4
{
public string start { get; set; }
public string end { get; set; }
}
public class Telecom
{
public string system { get; set; }
public string value { get; set; }
public string use { get; set; }
public Period4 period { get; set; }
}
public class Content
{
public string contentType { get; set; }
public string language { get; set; }
public string __invalid_name__dat
a { get; set; }
public int size { get; set; }
public string hash { get; set; }
public string title { get; set; }
}
public class Photo
{
public Content content { get; set; }
}
public class Details
{
public List<Address> address { get; set; }
public List<Name> name { get; set; }
public Gender gender { get; set; }
public string birthDate { get; set; }
public List<Identifier> identifier { get; set; }
public List<Telecom> telecom { get; set; }
public List<Photo> photo { get; set; }
}
public class EnrollmentSummary
{
public string dateEnrolled { get; set; }
public string __invalid_name__en
roller { get; set; }
}
public class Self2
{
public string href { get; set; }
}
public class UnEnroll
{
public string href { get; set; }
}
public class PersonLink
{
public string href { get; set; }
}
public class PersonMatch
{
public string href { get; set; }
}
public class Links2
{
public Self2 self { get; set; }
public UnEnroll __invalid_name__un
enroll { get; set; }
public PersonLink personLink { get; set; }
public PersonMatch personMatch { get; set; }
}
public class Person
{
public Details details { get; set; }
public bool __invalid_name__e
nrolled { get; set; }
public EnrollmentSummary enrollmentSummary { get; set; }
public Links2 _links { get; set; }
}
public class Embedded
{
public List<Person> person { get; set; }
}
public class RootObject
{
public SearchParameters searchParameters { get; set; }
public string message { get; set; }
public LinKs __invalid_name___lin
ks { get; set; }
public Embedded _embedded { get; set; }
}
The following function will convert JSON into a C# class where T is the class type.
public static T Deserialise<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms); //
return obj;
}
}
You need a reference to the System.Runtime.Serialization.json namespace.
The function is called in the following manner;
calendarList = Deserialise<GoogleCalendarList>(calendarListString);
calendarlist being the C# class and calendarListString the string containing the JSON.

Handling JSON variable without property

I have next JSON:
"jsonrpc":"2.0",
"id":1,
"result":
{
"1375833600000":
{
"notifications":[],
"events":[],
"lectures":[],
"birthdays":[
{
"id":"BB390BEE-CCBE-4D42-849C-E5040D6FD12C",
"date":1375909200000,
"name":"Test test",
"previewPhoto":"https://link.com/public/photos/h66/BB390BEE-CCBE-4D42-849C-E5040D6FD12C.jpg"
}]
},
"1375833600001":
{
"notifications":[],
"events":[],
"lectures":[],
"birthdays":[
{
"id":"BB390BEE-CCBE-4D42-849C-E5040D6FD12C",
"date":1375909200000,
"name":"Test test",
"previewPhoto":"https://link.com/public/photos/h66/BB390BEE-CCBE-4D42-849C-E5040D6FD12C.jpg"
}]
},
I don't understand what to write for the string property in result:
class Schedule
{
[JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("result")]
public Result Result { get; set; }
}
public class Result
{
How to handle this: "1375833600000"
"1375833600001"
}
I understand that it is array like Result, but it has different names
I have tried this:
class Schedule
{
[JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
private List<Results> result = new List<Results>();
[JsonProperty("result")]
public List<Results> Result { get { return result; } }
}
public class Results
{
private List<Event> events = new List<Event>();
[JsonProperty("events")]
public List<Event> Events { get { return events; } }
private List<Birthday> birthdays = new List<Birthday>();
[JsonProperty("birthdays")]
public List<Birthday> Birthdays { get { return birthdays; } }
}
public class Event
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("datetime")]
public long Datetime { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("allDayEvent")]
public bool AllDayEvent { get; set; }
}
public class Birthday
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("date")]
public long Date { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("previewPhoto")]
public string PreviewPhoto { get; set; }
}
But it fails
I agree it's a bit of a crappy way to represent a data structure, but it is still valid json, see enter link description here
I think the Dictionary type would de-serialize this okay assuming you are using newtonsoft's Json.net .
public class Schedule
{
[JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("result")]
public Dictionary<string, Datas> Result { get; set; }
}
public class Datas
{
public IEnumerable<string> Notifications { get; set; }
public IEnumerable<string> Events { get; set; }
public IEnumerable<string> Lectures { get; set; }
}

Categories