{
"id": "zd_tags",
"value": [
"bp_ticket;pazure_server"
]
},
{
"id": "itd_priority_1",
"value": 900
},
{
"id": "impacted_services",
"value": [
"Serverdown0208_2"
]
},
I am unable to read and deserialize the JSON into my objects which throws the below error.
"$.incidentTags[6].value": [
"The JSON value could not be converted to System.String[]. Path: $.incidentTags[6].value | LineNumber: 0 | BytePositionInLine: 1856."
]
Below is my current structure to deserialize it.
public class incidentTag
{
public string id { get; set; }
public string[] value {get;set;}
}
Here is the problem, Some tag has no array " "id": "itd_priority_1" but many have an array of value.
What is the best way to handle this scenario?
CodePudding user response:
Cant make a comment so having to make an answer.
Previously I have used a dynamic for if the data is unstructured or I don't know the data structure. An example of how I have used a dynamic is below
FeedResponse<dynamic> resultSet = await feedIterator.ReadNextAsync();
foreach (dynamic item in resultSet)
{
tasks.Add(_targetContainer.CreateItemAsync(item.Something));
}
I presume similar could be applied to your use case
CodePudding user response:
Either you can create a custom json converter to handle the parsing the way you want it, there are many examples you can look for it.
Or as an altenative you can use JObject
, something like this, parse the json and visit the jproperty in jobject and check whether that is array or object or any other primitive type.
JObject jObject = JObject.parse(your json);
foreach (var property in jObject.Properties())
{
VisitToken(property.Value);
}
private void VisitToken(JToken token)
{
switch (token.Type)
{
case JTokenType.Object:
VisitJObject(token.Value<JObject>());
break;
case JTokenType.Array:
VisitArray(token.Value<JArray>());
break;
case JTokenType.Integer:
case JTokenType.Float:
case JTokenType.String:
case JTokenType.Boolean:
case JTokenType.Bytes:
case JTokenType.Raw:
case JTokenType.Null:
VisitPrimitive(token);
break;
default:
throw new FormatException($"Invalid JSON token: {token}");
}
}
CodePudding user response:
Old Post for fixing this issue
The Above link helped me to resolve the problem and one more thing is, need to add the reference for "Microsoft.AspNetCore.Mvc.NewtonsoftJson;".I guess, .Net core 6 is using the System.text.Json which has an issue or does not support this operation.