Reputation: 11364
We have data coming from 3rd party system and I have one class designed like this,
public class CollectionProperty
{
public string Name { get; set; }
public object Value { get; set; }
}
and my VS debugger saying value like this which gives me extra details of data type along with result,
Now when I serialize this using Newtonsoft,
var x = JsonConvert.SerializeObject(resultPacket);
it's giving below output with only value kind and NOT the value.
I need string value with double quote, number value withOUT double quote, how to do this?
[{"Name":"Device ID","Value":{"ValueKind":3}}]},{"Name":"CPU0","CollectionProperties":[{"Name":"CPU Load Percentage","Value":{"ValueKind":4}}]}]
Upvotes: 5
Views: 8206
Reputation: 10078
Looks like the issue came from mix of multiple JSON
serializers, and System.Text.Json
seemed to be the issue in my case.
I was using Newtonsoft.Json
for all JSON handling, but request data to controllers were already coming with the ValueKind
thing whenever the model had anything with type Object
, which was messing up things in the code.
To fix this, I made Newtonsoft.Json
the default JSON serializer for the controllers as well
Microsoft.AspNetCore.Mvc.NewtonsoftJson
Program.cs
configurebuilder.Services.AddControllers().AddNewtonsoftJson();
Upvotes: 1
Reputation: 10305
This happens when you are mixing serializers. I had this issue as well. Basically, I was deserializing an object using System.Text.Json
and then a 3rd party library we were using was serializing the objects using Newtonsoft.Json
. This will yield the result that you are seeing.
In my case, since I was using a 3rd party library I could not change how they were serializing, so I basically just made a function to turn all of my class values into a string before serializing it again. This might not be possible for everyone, but since I know my data can be converted to a string, that is how I approached this.
Upvotes: 1
Reputation: 41
I had the same issue, the only way I found was to use System.Text.Json for serializing :
string json = System.Text.Json.JsonSerializer.Serialize(resultPacket);
The ValueKind property comes when using dynamic objects and expandoObject in my case. Hope this helps!
Upvotes: 4
Reputation: 214
Assuming this is a valid incoming json,
[{"Name":"CPU Load Percentage","Value":{"ValueKind":4}}]
Change your class to look like this. You can change the class /property name as per your need. Json Attribute is to map your property with incoming JSON property.
Also, you can use dynamic
or JObject
if your incoming JSON property can change in future. If it wont, you can use the code below:
public class Value {
[JsonProperty("ValueKind")]
public int ValueKind;
}
public class SystemData{
[JsonProperty("Name")]
public string Name;
[JsonProperty("Value")]
public Value Value;
}
//Your root class.
public class CollectionProperty{
public List<SystemData> SystemDataList;
}
Since your json has [{}] that means , its an array or collection of complex objects. {} = class so it seems, there comes a list of class with two properties Name and Value in which again Value is a class or complex object with a property ValueKind of type int.
CollectionProperty myDeserializedClass = JsonConvert.DeserializeObject<CollectionProperty>(myJsonResponse);
Upvotes: 1