Json.NET issues

时间:2015-07-08 15:49:14

标签: c# json json.net

I'm trying to serialize a simple object in C#, send it, and then deserialize it on the other end. The first two parts seem to work fine, but when I try to do:

ReturnItem jsonval = JsonConvert.DeserializeObject<ReturnItem>(result);

I get:

07-08 11:41:15.519 I/MonoDroid( 2856): Newtonsoft.Json.JsonSerializationException: Error converting value "{"Val1":-1.0,"Val2":-1.0,"Val3":-1.0,"Val4":-1.0}" to type 'AndroidDemo.ReturnItem'. Path '', line 1, position 104. ---> System.ArgumentException: Could not cast or convert from System.String to AndroidDemo.ReturnItem.

For reference, ReturnItem looks like:

public class ReturnItem
{
    public double Val1{ get; set; }
    public double Val2{ get; set; }
    public double Val3{ get; set; }
    public double Val4{ get; set; }
}

result is a string that looks like:

"{\"Val1\":-1.0,\"Val2\":-1.0,\"Val3\":-1.0,\"Val4\":-1.0}"

Edit: Here is how I initially serialized ReturnItem.

ReturnItem value = new ReturnItem();
value.Val1= -1;
value.Val2= -1;
value.Val3= -1;
value.Val4= -1;
return JsonConvert.SerializeObject(value);

2 个答案:

答案 0 :(得分:3)

Do the double quotes around the JSON object exist in the string you're trying to deserialise?

If so then it's going to think the entire thing is a JSON representation of a string, rather than a 'ReturnItem'.

答案 1 :(得分:0)

确保AndroidDemo.ReturnItem类与上例中的ReturnItem类相同。