There are a few problems with the json string (i am assuming you are using C#). First of the values of the data
property are actually a string (given the quotes). Which is why you are getting the error.
What you could do is create a property to hold the string and then another property to parse the float
values after parsed. This is done by using a few JSON.Net attributes of JsonProperty
and JsonIgnore
. These attributes tell the serializer what the property name is in the json string and which properties to ignore.
class Foo
{
string dataString;
float[] data = null;
[JsonProperty("abc")]
public string Abc { get; set; }
[JsonProperty("data")]
public string DataString
{
get { return dataString; }
set
{
data = null;
dataString = value;
}
}
[JsonIgnore]
public float[] Data
{
get
{
if (data != null)
return data;
return data = dataString != null ? JsonConvert.DeserializeObject<float[]>(DataString) : null;
}
set
{
DataString = value == null ? null : JsonConvert.SerializeObject(value);
}
}
}
This looks like alot of code however uses backing fields to allow you to change both the Data
field and the DataString
field while maintaining a working serializer.
This is then simply called using
var i = JsonConvert.DeserializeObject<Foo>(test);