I have been given a REST service which provides some strange names, see json fomart below.
I have created some classes to map them to JSON using the following line:
var data = JsonConvert.DeserializeObject<SSTR)(jsond).inhoud_b.ToArray();
I have created classes such as:
Class inhoud_b {...}
Class bidt{...}
etc.
Json:
{
"nmbr": 0,
"tp": 1,
"sort": {
"sorted": false,
"unsorted": true
},
"inhoud_b":[
{
"bidt": {
"id": "144a207b-dd48-4ebe-87cb-153e3c6179c4",
"knldge": null,
"lTT": null,
"lD": {
"id": "52",
"text": "EM"
}
}
},
{
"bidt": {
"id": "155B307b-dd48-4ebe-87cb-125487856579",
"knldge": null,
"lTT": null,
"lDg": {
"id": "523",
"text": "AM"
}
}
}
],
"first": true,
"totalElements": 2
}
My question is, how can I map these entities with my own more meaningful models and use my models in my own code rather than those entities which don't make any sense to other developers?
Thank you.
You can use the JsonPropertyAttribute to annotate your model properties with their corresponding property name in the JSON - https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm
Related
I'm trying to deserialize json-formatted response below.
{
"context": "xxxxxx"
"value": [
{
"Id": "123"
"Time": "2022-12-01"
}
{
"Id": "123"
"Time": "2022-12-01"
}
....
]
}
According to this: https://www.newtonsoft.com/json/help/html/deserializeobject.htm, this code should work.
public class WorkingSetContent
{
/// <summary>Collection ID</summary>
[JsonProperty("context")]
public string Context { get; set; }
/// <summary>UserRelationship</summary>
[JsonProperty("value")]
public IList<ItemClass> Items { get; set; }
}
But I'm getting a build error : "Change 'Items' to be read-only by removing the property setter."
I changed the setter to private to avoid this build error, then I was able to run it, but it causes a runtime error as null value is passed.
This is the code analysis rule you're running up against.
One option is to initialize your collection property with an empty list:
[JsonProperty("value")]
public IList<ItemClass> Items { get; } = new List<ItemClass>();
However, the code analysis rule explicitly says:
You can suppress the warning if the property is part of a Data Transfer Object (DTO) class.
Since you're deserializing this class from JSON, I think it's safe to assume it's a DTO. I would recommend using a pattern in your .globalconfig or .editorconfig files to suppress this rule for all of your DTO model classes.
You have to fix your json
{
"context": "xxxxxx",
"value": [
{
"Id": "123",
"Time": "2022-12-01"
},
{
"Id": "123",
"Time": "2022-12-01"
}
]
}
Object1:
{
"UserId": 123,
"Name": "testuser",
"Roles": [
{
"UserRole": "Admin"
},
{
"UserRole": "NonAdmin"
}
]
}
Object2:
{
"Id": 123,
"EmpName": "testuser",
"UserRoles": [
{
"EmpRole": "Admin"
},
{
"EmpRole": "NonAdmin"
}
]
}
I'm trying to map this object in C# using automapper. It is working fine but we need to map this object dynamically without using C# code, like keep this properties in Json file or database. Is there any other approach? We need to avoid code changes in C# if any new property is added or removed in any object, so we are looking to convert object1 to Object2 dynamically without code change.
I like the idea of Strongly Typed IDs and I am using it for some time. I use it also in DTOs without and major problems... till now. I have a use case where I need to generate JSON Schema from some classes which uses Strongly Type IDs like following:
[StronglyTypedId(StronglyTypedIdBackingType.Long)]
public partial struct TweetId
{
}
public class Tweet
{
public TweetId Id {get; set;}
}
So, as described, TweetID is represented as long base type.
The problem arises when I want to generate JSON Schema from this type. I am currently using Newtonsoft.Json.Schema (I also tried some others) using this code:
JSchemaGenerator generator = new JSchemaGenerator();
JSchema quotesSchema = generator.Generate(typeof(Tweet));
The result I get is:
{
"definitions": {
"Tweet": {
"type": [
"object"
],
"properties": {
"Id": {
"$ref": "#/definitions/TweetId"
}
}
},
"TweetId": {},
},
"type": "object",
"$ref": "#/definitions/Tweet"
}
But what I want is:
{
"definitions": {
"Tweet": {
"type": [
"object"
],
"properties": {
"Id": {
"type": "integer"
}
}
}
},
"type": "object",
"$ref": "#/definitions/Tweet"
}
Do you have any idea how to achive this? Newtonsoft.Json.Schema is not mendatory.
I am looking at one WebAPI application sample that has this coded:
json.SerializerSettings.PreserveReferencesHandling
= Newtonsoft.Json.PreserveReferencesHandling.Objects;
and another with this coded:
json.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Neither explain why each is chosen. I'm very new to WebAPI, so can someone help by explaining to me in simple terms what the differences are and why I might need to use one over the other.
These settings can best be explained by example. Let's say that we want to represent a hierarchy of employees in a company. So we make a simple class like this:
class Employee
{
public string Name { get; set; }
public List<Employee> Subordinates { get; set; }
}
This is a small company with only three employees so far: Angela, Bob and Charles. Angela is the boss, while Bob and Charles are her subordinates. Let's set up the data to describe this relationship:
Employee angela = new Employee { Name = "Angela Anderson" };
Employee bob = new Employee { Name = "Bob Brown" };
Employee charles = new Employee { Name = "Charles Cooper" };
angela.Subordinates = new List<Employee> { bob, charles };
List<Employee> employees = new List<Employee> { angela, bob, charles };
If we serialize the list of employees to JSON...
string json = JsonConvert.SerializeObject(employees, Formatting.Indented);
Console.WriteLine(json);
...we get this output:
[
{
"Name": "Angela Anderson",
"Subordinates": [
{
"Name": "Bob Brown",
"Subordinates": null
},
{
"Name": "Charles Cooper",
"Subordinates": null
}
]
},
{
"Name": "Bob Brown",
"Subordinates": null
},
{
"Name": "Charles Cooper",
"Subordinates": null
}
]
So far so good. You'll notice, however, that the information for Bob and Charles is repeated in the JSON because the objects representing them are referenced both by the main list of employees and Angela's list of subordinates. Maybe that's OK for now.
Now suppose we'd also like to have a way to keep track of each Employee's supervisor in addition to his or her subordinates. So we change our Employee model to add a Supervisor property...
class Employee
{
public string Name { get; set; }
public Employee Supervisor { get; set; }
public List<Employee> Subordinates { get; set; }
}
...and add a couple more lines to our setup code to indicate that Charles and Bob report to Angela:
Employee angela = new Employee { Name = "Angela Anderson" };
Employee bob = new Employee { Name = "Bob Brown" };
Employee charles = new Employee { Name = "Charles Cooper" };
angela.Subordinates = new List<Employee> { bob, charles };
bob.Supervisor = angela; // added this line
charles.Supervisor = angela; // added this line
List<Employee> employees = new List<Employee> { angela, bob, charles };
But now we have a bit of a problem. Because the object graph has reference loops in it (e.g. angela references bob while bob references angela), we will get a JsonSerializationException when we try to serialize the employees list. One way we can get around this issue is by setting ReferenceLoopHandling to Ignore like this:
JsonSerializerSettings settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(employees, settings);
With this setting in place, we get the following JSON:
[
{
"Name": "Angela Anderson",
"Supervisor": null,
"Subordinates": [
{
"Name": "Bob Brown",
"Subordinates": null
},
{
"Name": "Charles Cooper",
"Subordinates": null
}
]
},
{
"Name": "Bob Brown",
"Supervisor": {
"Name": "Angela Anderson",
"Supervisor": null,
"Subordinates": [
{
"Name": "Charles Cooper",
"Subordinates": null
}
]
},
"Subordinates": null
},
{
"Name": "Charles Cooper",
"Supervisor": {
"Name": "Angela Anderson",
"Supervisor": null,
"Subordinates": [
{
"Name": "Bob Brown",
"Subordinates": null
}
]
},
"Subordinates": null
}
]
If you examine the JSON, it should be clear what this setting does: any time the serializer encounters a reference back to an object it is already in the process of serializing, it simply skips that member. (This prevents the serializer from getting into an infinite loop.) You can see that in Angela's list of subordinates in the top part of the JSON, neither Bob nor Charles show a supervisor. In the bottom part of the JSON, Bob and Charles both show Angela as their supervisor, but notice her subordinates list at that point does not include both Bob and Charles.
While it is possible to work with this JSON and maybe even reconstruct the original object hierarchy from it with some work, it is clearly not optimal. We can eliminate the repeated information in the JSON while still preserving the object references by using the PreserveReferencesHandling setting instead:
JsonSerializerSettings settings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(employees, settings);
Now we get the following JSON:
[
{
"$id": "1",
"Name": "Angela Anderson",
"Supervisor": null,
"Subordinates": [
{
"$id": "2",
"Name": "Bob Brown",
"Supervisor": {
"$ref": "1"
},
"Subordinates": null
},
{
"$id": "3",
"Name": "Charles Cooper",
"Supervisor": {
"$ref": "1"
},
"Subordinates": null
}
]
},
{
"$ref": "2"
},
{
"$ref": "3"
}
]
Notice that now each object has been assigned a sequential $id value in the JSON. The first time that an object appears, it is serialized in full, while subsequent references are replaced with a special $ref property that refers back to the original object with the corresponding $id. With this setting in place, the JSON is much more concise and can be deserialized back into the original object hierarchy with no additional work required, assuming you are using a library that understands the $id and $ref notation produced by Json.Net / Web API.
So why would you choose one setting or the other? It depends on your needs of course. If the JSON will be consumed by a client that does not understand the $id/$ref format, and it can tolerate having incomplete data in places, you would choose to use ReferenceLoopHandling.Ignore. If you're looking for more compact JSON and you will be using Json.Net or Web API (or another compatible library) to deserialize the data, then you would choose to use PreserveReferencesHandling.Objects. If your data is a directed acyclic graph with no duplicate references then you don't need either setting.
The explaination is perfect. For me the below one worked, data is your object.
However, if the above-mentioned method did not work, you can try this one:
string json = JsonConvert.SerializeObject(data, Formatting.Indented,new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
I am getting following json format after hitting to an API:
{
"7407": {
"survey_id": "406",
"device_id": "1",
"response_time": "2013-10-10 16:14:01",
"timezone": "0",
"language_id": "en",
"response_id": "7407",
"device_alias": "QR Code App",
"site_name": "QR Code App",
"country_name": "United States",
"state_name": "New York",
"city_name": "Suffern",
"zip": "",
"voucher_name": null,
"voucher_mode": null,
"surveyee_name": null,
"surveyee_email": null,
"surveyee_phone": null,
"ques": {
"": []
}
},
"7408": {
"survey_id": "406",
"device_id": "1",
"response_time": "2013-10-10 16:36:56",
"timezone": "0",
"language_id": "en",
"response_id": "7408",
"device_alias": "QR Code App",
"site_name": "QR Code App",
"country_name": "India",
"state_name": "Gujarat",
"city_name": "Ahmedabad",
"zip": "",
"voucher_name": null,
"voucher_mode": null,
"surveyee_name": null,
"surveyee_email": null,
"surveyee_phone": null,
"ques": {
"": []
}
} }
I am using JSON.Net to read the above given json data.
To map this data into .Net code, I will need classes in .net, having same properties' name as in json string.
BUT there are some attributes in json which can be dynamic ("7407", "7408" etc in my case) i.e. this value can be changed based on what are we passing into parameters.
My question is, how can we map json attributes (which are dynamic in nature and can have any value depending upon the parameters provided to the apis) to our .net class ?
You could map it to a Dictionary. Where your dynamic property is the Key of a DictionaryIem and the Objet is the Value of your DictionaryItem.
For Exmaple:
public class MyClass
{
public void readJson)
{
var json = "{\"7407\": {\"survey_id\": \"406\",\"device_id\": \"1\",},\"7408\": {\"survey_id\": \"406\",\"device_id\": \"1\",}}";
Dictionary<int, MyObject> dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<int, MyObject>>(json);
var count = dict.Keys.Count;
}
}
public class MyObject
{
public string survey_id { get; set; }
public string device_id { get; set; }
}
For this exmaple i simplified your json. so that it looks like this:
{
"7407": {
"survey_id": "406",
"device_id": "1"
},
"7408": {
"survey_id": "406",
"device_id": "1"
}
}
Js object is a dictionary, so you can map to Dictionary with key = some attribute, and value - .Net code what you want
Dictionary<object,'your data class'>
The best way I could find handling this kind of JSON data is by using JObject class of the JSON.Net library.
For eg:
Newtonsoft.Json.Linq.JObject jSonObject = JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(somejsonstring);
and then you can loop through or apply some other logic to read jSonObject