我的任务是将JSON解析为可以创建表单的对象。这种形式可以在它的子形式,这是我有困难的这一步。考虑下面这个jobobject,它是子表单的表示形式,由于主表单已经被反序列化,所以现在作为jobobject传递:
{
"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继承了转换器。CustomCreationConverter (DBFormField)
Public Overrides Function Create(objectType As Type) As DBFormField
Return New DBFormField()
End Function
End Class
我怀疑将。tostring放在JObject的末尾对于DeserializeObject方法来说不够好?请看上面的评论。但是这段代码抛出错误…
不能反序列化当前JSON对象(例如{"name":"value"})into type 'System.Collections.Generic.List ' 1[RSAP.DBFormField]'因为该类型需要一个JSON数组(例如[1,2,3])来反序列化正确。
我没有尝试将jobobject序列化为JSON字符串-考虑到我们已经将JSON解析为jobobject,这似乎很愚蠢。
您在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);
[
和]
来封装json对象。