C
C#7mo ago
OkOk

✅ "Paste JSON as classes" functionality in VS

If your JSON has two keys of the same name with different values inside, should you let VS create multiple classes called "exampleClass" and "exampleClass1" or should you create one single class that contains all of these properties? Example JSON: "{ "results": [ { "id": "73827820", "_links": { "webui": "/pages/viewpage.action?pageId=73827820", "edit": "/pages/resumedraft.action?draftId=73827820&draftShareId=f7777609-f3b9-4875-8904-e3b99c6870b4", "tinyui": "/x/7IVmB", "self": "https://confluence.vll.se/rest/api/content/73827820" }, } ], "start": 0, "limit": 25, "size": 1, "_links": { "self": "https://confluence.vll.se/rest/api/content?spaceKey=~LONO05&title=Tj%C3%A4nstedom%C3%A4ner2", "base": "https://confluence.vll.se", "context": "" } }" Pasting this will make VS generate the classes public class _Links { public string self { get; set; } public string _base { get; set; } public string context { get; set; } } and public class _Links1 { public string webui { get; set; } public string edit { get; set; } public string tinyui { get; set; } public string self { get; set; } } Should I instead create only the class "_Links" and have it contain all of the combined properties like so? public class _Links { public string self { get; set; } public string _base { get; set; } public string context { get; set; } public string webui { get; set; } public string edit { get; set; } public string tinyui { get; set; } public string self { get; set; } }
2 Replies
Pobiega
Pobiega7mo ago
You should never trust the output of "paste as classes", it almost always needs to be manually adjusted in this case, you have to decide if these should be the same class, two different classes, or a class hierarchy with discriminator
OkOk
OkOk7mo ago
Thank you Pobiega #close