I have a noSQL database. When I'm trying to map the JSON data with C# class models, some of my data get mapped, but some don't.
Below is my Sample JSON format.
{
"J1D0GhKmzAT4gRn5VkfPKKVCfku2": {
"reports": {
"-KYbi7tbJoZJmCs8hcHy": {
"age": "0",
"description": "Test",
"incident": "Test",
"location": "Test"
},
"-KYbmoWJwzSSS0llsSZN": {
"age": "0",
"description": "Test",
"incident": "Test",
"location": "Test"
},
"-KYbszjzkYnH2N9xbFMJ": {
"age": "0",
"description": "Test",
"incident": "Test",
"location": "Test"
}
},
"user_info": {
"dob": "Feb 11, 2016",
"name": "Test",
"phone": "44444",
"sex": "Male",
"work": "llllll"
}
},
"JxmpIWWioFbg1Po4gXtV07pwDvX2": {
"reports": {
"-KYiiDRl7fYAPdav13h3": {
"age": "0",
"description": "Test",
"incident": "Test",
"location": "Test"
},
"-KYinWZeP7N24x8QUC6O": {
"age": "0",
"description": "Test",
"incident": "Test",
"location": "Test"
}
},
"user_info": {
"dob": "Feb 11, 2016",
"name": "Test",
"phone": "44444",
"sex": "Male",
"work": "llllll"
}
}
}
and My C# Class Models are as below
public class User
{
public user_info user_info { get; set; }
public reports reports { get; set; }
}
public class user_info
{
public string dob { get; set; }
public string name { get; set; }
public string phone { get; set; }
public string sex { get; set; }
public string work { get; set; }
}
public class reports
{
public List<reportInfo> reportInfo { get; set; }
}
public class reportInfo
{
public string age { get; set; }
public string description { get; set; }
public string incident { get; set; }
public string location { get; set; }
}
Here, when I try to map the JSON with C# classes, only the user_info model gets populated for some reason. There is a matching property in JSON. But my reports model doesn't get populated, because it has some dynamic properties which is not getting mapped with model.
Please let me know where I am going wrong and the possible solution.
Thanks in advance.
Following dbc advice, you should modify your model.
Delete the reports class and modify User class like this:
public class User
{
public user_info user_info { get; set; }
public Dictionary<string, reportInfo> reports { get; set; }
}
Related
The problem I am facing is that I am getting 0's for the values I ask for in the JSON.
I recently asked a question here about getting specific values from a JSON array response that come from an API.
Now I have a new JSON response from a different route and in there I want one values and its called EntityId its places in a block of Entity See the code for more details, I would like to grab that from this response and place them in a list or Array because it tells me who submitted the assignment in D2L.
UPDATE- After getting comments from a user I used JSON to C# tool to create the full class with all the values but I still get the values as 0,0,0,0.
Right now I am getting 0,0,0,0 values and not getting the actual values if it's possible I would like to grab the whole block on Entity.
JSON
[
{
"Entity": {
"DisplayName": "FullName",
"EntityId": 123,
"EntityType": "User",
"Active": true
},
"Status": 1,
"Feedback": null,
"Submissions": [
{
"Id": 27,
"SubmittedBy": {
"Identifier": "123",
"DisplayName": "FullName"
},
"SubmissionDate": "2019-08-01T15:25:04.130Z",
"Comment": {
"Text": "",
"Html": ""
},
"Files": [
{
"IsRead": false,
"IsFlagged": false,
"IsDeleted": false,
"FileId": 1245,
"FileName": "1.2.10.png",
"Size": 144407
},
{
"IsRead": false,
"IsFlagged": false,
"IsDeleted": false,
"FileId": 292,
"FileName": "1.3.8.png",
"Size": 127869
}
]
}
],
"CompletionDate": "2019-08-01T15:25:04.130Z"
},
{
"Entity": {
"DisplayName": "FullName",
"EntityId": 123,
"EntityType": "User",
"Active": true
},
"Status": 1,
"Feedback": null,
"Submissions": [
{
"Id": 41,
"SubmittedBy": {
"Identifier": "123",
"DisplayName": "FullName"
},
"SubmissionDate": "2019-08-03T03:31:43.807Z",
"Comment": {
"Text": " \nAlex",
"Html": "<p></p>\n<p>Alex</p>"
},
"Files": [
{
"IsRead": false,
"IsFlagged": false,
"IsDeleted": false,
"FileId": 313,
"FileName": "Capture 1.2.10 Questions.PNG",
"Size": 97722
}
]
}
],
"CompletionDate": "2019-08-03T03:31:43.807Z"
}
]
Classes:
public class Entity
{
public string DisplayName { get; set; }
public int EntityId { get; set; }
public string EntityType { get; set; }
public bool Active { get; set; }
}
public class SubmittedBy
{
public string Identifier { get; set; }
public string DisplayName { get; set; }
}
public class Comment
{
public string Text { get; set; }
public string Html { get; set; }
}
public class File
{
public bool IsRead { get; set; }
public bool IsFlagged { get; set; }
public bool IsDeleted { get; set; }
public int FileId { get; set; }
public string FileName { get; set; }
public int Size { get; set; }
}
public class Submission
{
public int Id { get; set; }
public SubmittedBy SubmittedBy { get; set; }
public DateTime SubmissionDate { get; set; }
public Comment Comment { get; set; }
public IList<File> Files { get; set; }
}
public class Example
{
public Entity Entity { get; set; }
public int Status { get; set; }
public object Feedback { get; set; }
public IList<Submission> Submissions { get; set; }
public DateTime CompletionDate { get; set; }
}
Code:
var request = new RestRequest(string.Format(Link));
request.Method = Method.GET;
authenticator.Authenticate(client, request);
var response = client.Execute(request);
var thisasa = JsonConvert.DeserializeObject<List<Example>>
(response.Content).Select(
o => o.Identifier).ToList();
The Example data model shown in your question already can be used to successfully deserialize the JSON shown. All that's left is to pick out the Entity.EntityId property via a Select:
var thisasa = JsonConvert.DeserializeObject<List<Example>>(response.Content)
.Select(o => o.Entity.EntityId)
.ToList();
Demo fiddle #1 here.
Incidentally, if you only need Entity.EntityId you could simplify your data model as follows:
public class Entity
{
public int EntityId { get; set; }
}
public class Example
{
public Entity Entity { get; set; }
}
Demo fiddle #2 here.
(As an aside, since your Example class corresponds to the documented object Dropbox.EntityDropbox, you might want to rename your type to EntityDropbox for clarity.)
I have to:
DeserializeObject - do some changes - SerializeObject
When my Json have miltiple types e.g.
{
"type": "test",
"currentStatus": "Active",
"id": "987",
"items": [
{
"type": "test1",
"id": "123",
"name": "Segment Members",
"memberCount": "0",
"outputTerminals": [
{
"type": "test2",
"id": "123",
"connectedId": "123",
"terminalType": "out"
}
],
"position": {
"type": "Position",
"x": "46",
"y": "14"
},
"isFinished": "true",
"isRecurring": "false",
"segmentId": "123"
},
{
"type": "test5",
"id": "1390",
"name": "Yay! Clickers",
"memberCount": "2",
"position": {
"type": "Position",
"x": "330",
"y": "375"
},
"waitFor": "2592000"
},
{
"type": "test3",
"id": "1391",
"name": "test",
"memberCount": "73",
"outputTerminals": [
{
"type": "test4",
"id": "123",
"connectedId": "123",
"connectedType": "CampaignWaitAction",
"terminalType": "yes"
},
{
"type": "test4",
"id": "123",
"connectedId": "123",
"connectedType": "CampaignWaitAction",
"terminalType": "no"
}
],
"position": {
"type": "Position",
"x": "123",
"y": "123"
},
"testId": "123"
}
]
}
What data type i should use for this operation? dynamic, object, Jobject...? or maybe something else?
I would just create an object and deseriablize it to that type. A lot easier than doing it dynamically. (assuming it will stay in that structure always)
WHAT TO DO
Copy your Json:
`Edit` -> `Paste Special` -> `Paste JSON As CLASSES`
And there you go! You have the Type you want to deserialize to.
var deserializedJson = JsonConvert.DeserializeObject<YourNewObject>(jsonString);
Note: if the pasting of the json class doesn't work, make sure your json is valid: ClickHereToValidateJson
using Newtonsoft.Json;
dynamic collection = new
{
MultipleType = //call function which returns json here
};
//serialize
var jsonSerializedFeed = JsonConvert.SerializeObject(MultipleType);
//deserialize
var jsonSerializedFeed = JsonConvert.DeserializeObject(MultipleType);
Hope it helps
You can create classes and serialize/deserialize json:
public class OutputTerminal
{
public string type { get; set; }
public string id { get; set; }
public string connectedId { get; set; }
public string terminalType { get; set; }
public string connectedType { get; set; }
}
public class Position
{
public string type { get; set; }
public string x { get; set; }
public string y { get; set; }
}
public class Item
{
public string type { get; set; }
public string id { get; set; }
public string name { get; set; }
public string memberCount { get; set; }
public IList<OutputTerminal> outputTerminals { get; set; }
public Position position { get; set; }
public string isFinished { get; set; }
public string isRecurring { get; set; }
public string segmentId { get; set; }
public string waitFor { get; set; }
public string testId { get; set; }
}
public class Root
{
public string type { get; set; }
public string currentStatus { get; set; }
public string id { get; set; }
public IList<Item> items { get; set; }
}
And then:
string json = JsonConvert.SerializeObject(rootObject);
Root root = JsonConvert.DeserializeObject<Root>(json);
Also, you can use dynamic type without classes:
string json = JsonConvert.SerializeObject(dynamicObject);
dynamic obj = JsonConvert.DeserializeObject(json);
I cannot deserialized the following data which I'm using for testing and got it from the World Bank using the following query:
http://api.worldbank.org/countries/IRL/indicators/SP.DYN.CBRT.IN?
per_page=10&date=1960:2016&format=json
[
{
"page": 1,
"pages": 28,
"per_page": "2",
"total": 56
},
[
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.2",
"decimal": "0",
"date": "1961"
},
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.5",
"decimal": "0",
"date": "1960"
}
]
]
My main class is called PageModel is defined as such:
public class PageModel
{
public PageModel()
{
this.List = new List<Data>();
}
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("pages")]
public int Pages { get; set; }
[JsonProperty("per_page")]
public string PerPage { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
public List<Data> List { get; set; }
}
The class used in the array is called Data and is defined as follows:
public class Data
{
public Data()
{
this.Indicator = new Indicator();
this.Country = new Country();
}
[JsonProperty("indicator")]
public Indicator Indicator { get; set; }
[JsonProperty("country")]
public Country Country { get; set; }
[JsonProperty("date")]
public int Date { get; set; }
[JsonProperty("value")]
public float Value { get; set; }
[JsonProperty("decimal")]
public decimal Decimal { get; set; }
}
Both the Country and Indicator classes are defined as follows:
public class Country
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class Indicator
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
My HttpClient call returns the data correctly but whenever I try to deserialize the data using the NewtonSoft JsonConvert.DeserializeObject function:
PageModel pageModel = JsonConvert.DeserializeObject<PageModel>(data);
It returns null.
Any ideas why?
Thanks.
Your JSON data is in wrong format:
Change your json to this and it will work:
{
"page": 1,
"pages": 28,
"per_page": "2",
"total": 56,
"List":[
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.2",
"decimal": "0",
"date": "1961"
},
{
"indicator": {
"id": "SP.DYN.CBRT.IN",
"value": "Birth rate, crude (per 1,000 people)"
},
"country": {
"id": "IE",
"value": "Ireland"
},
"value": "21.5",
"decimal": "0",
"date": "1960"
}
]
}
I wrote an application which is used to import new data from people into a database. The data I get is located on the web, that's working fine.
I used to work with the following structure:
{
"users": [
{
"firstname": "John",
"lastname": "Doe",
"id": "1",
"rfid": "BCDA412EA"
},
{
"firstname": "Jane",
"lastname": "Doe",
"id": "2",
"rfid": "DA412EBCA"
}
]
}
However, I got a new "structure" that I can not handle. The new nodes are random generated values? How can I access those?
New json looks like this:
{
"page": 0,
"page_size": 200,
"total_count": 5,
"count": 5,
"data": {
"ab90708-ded183ab37b55-623f-42ae-ae51": {
"firstname": "John",
"lastname": "Doe",
"created_at": "2015-09-16T15:51:39Z",
"tags": [
"803504",
"80363004",
"8436E64",
"test123"
]
},
"34ba-0619-4ed8-bf168d2a-ce3af684a2b0": {
"firstname": "Stefan",
"lastname": "Baloh",
"created_at": "2015-09-16T15:51:40Z",
"tags": [
"8034E26A4B0034004",
"F3626A4B0034035"
]
}
}
}
Is it even possible to work with such a structure?
You can declare your classes as
public class User
{
public string firstname { get; set; }
public string lastname { get; set; }
public string created_at { get; set; }
public List<string> tags { get; set; }
}
public class RootObject
{
public int page { get; set; }
public int page_size { get; set; }
public int total_count { get; set; }
public int count { get; set; }
public Dictionary<string, User> data { get; set; }
}
And deserialize as (using Json.Net)
var obj = JsonConvert.DeserializeObject<RootObject>(json);
foreach(var user in obj.data.Values)
{
Console.WriteLine(user.lastname);
}
The keyword here is the use of Dictionary<string,...> for these random names..
here is my json response that I am trying to deserialize
[{
"Musician": {
"id": "554343",
"Name": "16408",
"Age": "22"
},
"Albums": [{
"id": "34343",
"AlbumName": "Super charge",
"ReleaseDate": "",
"Hits": ""
}, {
"id": "34222",
"AlbumName": "Super 2",
"ReleaseDate": "",
"Hits": ""
}]
},
{
"Musician": {
"id": "554344",
"Name": "16468",
"Age": "32"
},
"Albums": [{
"id": "34323",
"AlbumName": "Awesome one",
"ReleaseDate": "",
"Hits": ""
}, {
"id": "34222",
"AlbumName": "Awesome two",
"ReleaseDate": "",
"Hits": ""
}]
}]
here are my class
public class Musician
{
public string id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
}
public class Album
{
public string id { get; set; }
public string AlbumName { get; set; }
public string ReleaseDate { get; set; }
public string Hits { get; set; }
}
public class RootObject
{
public Musician Musician { get; set; }
public List<Album> Albums { get; set; }
}
this is the code i am using to deserialize objects
var musicians = JsonConvert.DeserializeObject<List<Musician>>(data);
var album = JsonConvert.DeserializeObject<List<Album>>(data);
musicians variable shows 2 counts of record while debugging but value of elements id,Name,Age always shows null. Not able to recognize what exactly causing it.
You should be deserializing as collection of RootObjects.
var musicians = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(data);
Output