我想將一個json字符串轉換爲使用Newtonsoft.json的對象,但我遇到了下列轉換的一些問題。我想知道有人能解釋這一點。謝謝。C#使用Newtonsoft.Json將JSON字符串反序列化爲對象
AddFaceResponse ir = JsonConvert.DeserializeObject<AddFaceResponse>(responseContentStr);
這是JSON字符串responseContentStr
[{
"faceId": "1fe48282-a3b0-47d1-8fa8-67c4fac3d984",
"faceRectangle": {
"top": 80,
"left": 50,
"width": 147,
"height": 147
}
}]
這是我的模型對象。
public class AddFaceResponse
{
public class Face
{
public class FaceRectangle
{
public int top, left, width, height;
public FaceRectangle(int t, int l, int w, int h)
{
top = t;
left = l;
width = w;
height = h;
}
}
public string faceId;
public FaceRectangle faceRectangle;
public Face(string id, FaceRectangle fac)
{
faceId = id;
faceRectangle = fac;
}
}
Face[] faces;
public AddFaceResponse(Face[] f)
{
faces = f;
}
}
這是我從visual studio中得到的錯誤。
Newtonsoft.Json.JsonSerializationException:不能反序列化當前JSON陣列(例如[1,2,3])轉換成類型 'App2.AddFaceResponse',因爲類型需要JSON對象(例如 { 「名稱」 :「value」})以正確地反序列化
「IdentifyResponse」類的定義在哪裏。 –
對不起,我複製了錯誤的代碼行。我打算將字符串轉換爲AddFaceResponse。我只是更新它。 @TravisJ – EricMA