Why do i get an error when i try to deserialize this JSON:
{
"id" : 1,
"name" : "demo",
"pixeldata" : [
{ "time" : "1", "colour" : "#ff5357" },
{ "time" : "2", "colour" : "#2424ff" },
{ "time" : "3", "colour" : "#ff0d13" },
{ "time" : "4", "colour" : "#f7ff4a" },
{ "time" : "5", "colour" : "#fa24ff" },
{ "time" : "6", "colour" : "#ff3e43" }
]
}
into these classes:
public struct Display
{
public int id;
public string name;
public List<Pixeldata> pixeldata;
}
public struct Pixeldata
{
public float time;
public string colour;
}
using this:
Display MyDisplay = JsonConvert.DeserializeObject<Display>(json_string);
error says:
Error converting value "[{ "time" : "1", "colour" : "#ff5357"}, {etc...}]" to type 'System.Collections.Generic.List`1[Pixeldata]'. Path 'pixeldata', line 1, position 313.
I've been reading similar QnA's, looking at various examples and they all look like this should work, but it doesn't, what do i do?
Update: i used this code to dump my string
Debug.Print("JSON DUMP: "+_ServerResponse.data);
this is the exact result as it shows in the console
JSON DUMP: {"id":1,"name":"demo","pixeldata":"[{ \"time\" : \"1\", \"colour\" : \"#ff5357\"},{ \"time\" : \"2\", \"colour\" : \"#2424ff\"},{ \"time\" : \"3\", \"colour\" : \"#ff0d13\"},{ \"time\" : \"4\", \"colour\" : \"#f7ff4a\"},{ \"time\" : \"5\", \"colour\" : \"#fa24ff\"},{ \"time\" : \"6\", \"colour\" : \"#ff3e43\"}]"}
[0:]
"time" is of type string in your json, but you try to deserialize to type float. That won't work.
Update: Apparently, after reading the comments on your questions it would work in some implementations of Json.Net, but that might be "version-dependent", and hence might not work in your scenario.
The JSON i was receiving was not properly formatted.
I think it has something to do with how SQL stores JSON.
So i just messed around with decoding the values i got from the database then re-encoded them before sending, and now it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.