我的任务是将JSON解析为可以创建表单的对象。这个表格里面可以有子表格,这是我遇到困难的一步。 考虑这个JObject,它是子表单的表示,因为主表单已经反序列化,现在作为JObject传递:
{
"3705": {
"type": "radioGroup",
"label": "Loft Insulation Applicable",
"default_value": null,
"order": "2",
"required": "false",
"security": "false",
"option": null,
"child": [
{
"type": "radio",
"label": "No",
"order": "3"
},
{
"type": "radio",
"label": "Yes",
"order": "4"
}
]
},
"3708": {
"type": "input",
"label": "Existing Depth (mm)",
"default_value": null,
"order": "5",
"required": "false",
"security": "false",
"option": null
},
"3709": {
"type": "input",
"label": "Required Depth (mm)",
"default_value": null,
"order": "6",
"required": "false",
"security": "false",
"option": null
},
"3715": {
"type": "radioGroup",
"label": "Total Pipework Meterage",
"default_value": null,
"order": "16",
"required": "false",
"security": "false",
"option": null,
"child": [
{
"type": "radio",
"label": "15 mm",
"order": "17"
},
{
"type": "radio",
"label": "22 mm",
"order": "18"
},
{
"type": "radio",
"label": "28 mm",
"order": "19"
}
]
},
"3719": {
"type": "formLabel",
"label": "Loft Access Requirements",
"default_value": null,
"order": "20",
"required": "false",
"security": "false",
"option": null
},
"3720": {
"type": "radioGroup",
"label": "Crawlers",
"default_value": null,
"order": "21",
"required": "false",
"security": "false",
"option": null,
"child": [
{
"type": "radio",
"label": "No",
"order": "22"
},
{
"type": "radio",
"label": "Yes",
"order": "23"
}
]
},
"3723": {
"type": "radioGroup",
"label": "Cavity Ladders for Loft",
"default_value": null,
"order": "24",
"required": "false",
"security": "false",
"option": null,
"child": [
{
"type": "radio",
"label": "No",
"order": "25"
},
{
"type": "radio",
"label": "Yes",
"order": "26"
}
]
},
"3726": {
"type": "image",
"label": "Loft Photos",
"default_value": null,
"order": "27",
"required": "false",
"security": "false",
"option": null
}
}
我正在尝试将其反序列化为此类中定义的类型对象列表:
public class DBFormField
{
public string type { get; set; }
public string label { get; set; }
public string default_value { get; set; }
public string order { get; set; }
public string required { get; set; }
public DBFormFieldOption option = new DBFormFieldOption();
public List<DBFormFieldChild> child = new List<DBFormFieldChild>();
}
public class DBFormFieldOption
{
public List<DBFormFieldGrid> grid_rows = new List<DBFormFieldGrid>();
public List<DBFormFieldGrid> grid_columns = new List<DBFormFieldGrid>();
public string label1 { get; set; }
public string label2 { get; set; }
public string result_label { get; set; }
public string operation { get; set; }
public List<string> rows = new List<string>();
public string tab_id { get; set; }
}
public class DBFormFieldChild
{
public string type { get; set; }
public string label { get; set; }
public string order { get; set; }
}
public class DBFormFieldGrid
{
public string title { get; set; }
public string name { get; set; }
}
我尝试了一些策略来做到这一点。最新的,我正在尝试这个http://james.newtonking.com/json/help/CustomCreationConverter
例如:
List<DBFormField> parsedFields = new List<DBFormField>();
parsedFields = JsonConvert.DeserializeObject<List<DBFormField>>(someFormObj.form_fields.ToString, new FormConverter()); // ToString???
... 公共类FormConverter Inherits Converters.CustomCreationConverter(Of DBFormField)
Public Overrides Function Create(objectType As Type) As DBFormField
Return New DBFormField()
End Function
End Class
我怀疑将.ToString放在JObject的末尾对于DeserializeObject方法来说还不够好?见上面的评论。但是这段代码会引发错误......
无法反序列化当前的JSON对象(例如{“name”:“value”}) 进入类型'System.Collections.Generic.List`1 [RSAP.DBFormField]' 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确。
我还没有尝试将JObject序列化为JSON字符串 - 考虑到我们已经将JSON解析为Jobject,这似乎很糟糕。
答案 0 :(得分:0)
您在json中有单个对象,并且您尝试将其反序列化为对象列表 - 这是无法完成的。
解决方案:
[
和]
来将json对象包装在数组中
您可以反序列化为单个对象(不是列表):
JsonConvert.DeserializeObject<DBFormField>(someFormObj.form_fields.ToString(), new FormConverter());
您可以反序列化单个对象并将其添加到列表中:
List<DBFormField> parsedFields = new List<DBFormField>();
parsedField = JsonConvert.DeserializeObject<DBFormField>someFormObj.form_fields.ToString(), new FormConverter());
parsedFields.Add(parsedField);