Is this sample in a correct format based on JSON API specifications? In another word can we have in attributes an array?
{
"meta": {
},
"links": {
"self": ""
},
"jsonapi": {
"version": "",
"meta": {
}
},
"data": {
"type": "typeof(class)",
"id": "string",
"attributes": [
{
"item1": "Value1",
"item2": "Value2",
"item3": "Value3"
}
],
"links": {
"self": ""
}
}
}
I am not sure even after reading that (link) If correct how can I Deserialize it I am using JSONAPISerializer package in C#
CodePudding user response:
- Visit https://json2csharp.com/
- Paste your JSON. In Property Settings select Use Pascal Case.
- Copy class and paste into your project.
- Goto NuGet package manager and install newtonsoft json package.
- Use like,
var myDeserializedClass = JsonConvert.DeserializeObject<Root(myJsonResponse);
CodePudding user response:
Why aren't you sure? Here's a quote from JSON API specification:
Attributes may contain any valid JSON value, including complex data structures involving JSON objects and arrays.
Class System.Text.Json.JsonSerializer
can deserialize a JSON array into a C# IEnumerable<T>
. So you may create an object that has a property Attributes
of type IEnumerable<T>
and deserialize like this:
using System.IO;
using System.Text.Json;
// ...
string json = File.ReadAllText("YourJsonDocumentPath");
YourEntityDescribedInJsonDocument obj = JsonSerializer.Deserialize<YourEntityDescribedInJsonDocument>(json, new JsonSerializerOptions());