我正在尝试反序列化以下JSON。我从服务器获取此JSON,并且必须在桌面应用程序上使用它
{
"panicForms": [{
"name": "Name",
"description": "Description",
"title": "Title",
"Image": "",
"keyboardShortcut": "",
"instructions": "Instructions",
"submitButtonText": "",
"label": "",
"tooltip": "",
"textColor": "Black",
"backgroundColor": "Red",
"textStyle": "Normal",
"systemTrayEnabled": true,
"fields": [{
"eventField": "Name",
"label": "Full Name",
"type": "single selection",
"required": true
},
{
"eventField": "Age",
"label": "age",
"type": "single selection",
"required": false
}
]
}]
}
创建以下对象
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Field
{
public string eventField { get; set; }
public string label { get; set; }
public string type { get; set; }
public bool required { get; set; }
}
public class PanicForm
{
public string name { get; set; }
public string description { get; set; }
public string title { get; set; }
public string Image { get; set; }
public string keyboardShortcut { get; set; }
public string instructions { get; set; }
public string submitButtonText { get; set; }
public string label { get; set; }
public string tooltip { get; set; }
public string textColor { get; set; }
public string backgroundColor { get; set; }
public string textStyle { get; set; }
public bool systemTrayEnabled { get; set; }
public List<Field> fields { get; set; }
}
public class Root
{
public List<PanicForm> panicForms { get; set; }
}
并使用了以下代码进行反序列化
var stuff1 = JsonConvert.DeserializeObject<PanicForm[]>(jsonString1);
&
var stuff1 = JsonConvert.DeserializeObject<List<PanicForm>>(jsonString1);
&
var stuff1 = JsonConvert.DeserializeObject<Root>(jsonString1);
&
var stuff1 = JsonConvert.DeserializeObject<PanicForm>(jsonString1);
&
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(jsonString1);
但最后还是会得到同样的错误
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: s. Path '', line 0, position 0.'
详细是
StackTrace " at Newtonsoft.Json.JsonTextReader.ParseValue()\r\n at Newtonsoft.Json.JsonTextReader.Read()\r\n at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\r\n at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)\r\n at TestDynamicControls.Form1..ctor() in C:\\Alertus\\PanicUI\\TestDynamicControls\\TestDynamicControls\\Form1.cs:line 58\r\n at TestDynamicControls.Program.Main() in C:\\Alertus\\PanicUI\\TestDynamicControls\\TestDynamicControls\\Program.cs:line 19" string
谁能帮我解决这个问题,谢谢
2条答案
按热度按时间vwoqyblh1#
对于您公开的示例中的数据:
正确的方法是在你使用的工具的评论中告诉你:
听听这个小提琴:
https://dotnetfiddle.net/vofLpq
对于类:
你必须反序列化:
在此引用:
如果你有数据问题,你应该先检查你的输入,你可以使用任何json验证器:https://jsonlint.com/
vxqlmq5t2#
要获得一个列表,可以解析一个JSON字符串,然后去序列化一个数组