Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化对象错误:值不能为null_C#_Json_Json.net_Deserialization - Fatal编程技术网

C# 反序列化对象错误:值不能为null

C# 反序列化对象错误:值不能为null,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我在xamarin.forms应用程序中使用插件Newtonsoft.Json,对typrForm public class Form { [JsonProperty("id")] public int id { set; get; } [JsonProperty("title")] public string title { set; get; } [JsonProperty("body")] public string body { set

我在xamarin.forms应用程序中使用插件
Newtonsoft.Json
,对typr
Form

public class Form
{
    [JsonProperty("id")]
    public int id { set; get; }

    [JsonProperty("title")]
    public string title { set; get; }

    [JsonProperty("body")]
    public string body { set; get; }

    [JsonProperty("department_id")]
    public string department_id { set; get; }

    [JsonProperty("fields")]
    public List<FormField> fields { set; get; }
}

public class FormField
{
    [JsonProperty("id")]
    public int id { set; get; }

    [JsonProperty("title")]
    public string label { set; get; }

    [JsonProperty("inputtype")]
    public string inputtype { set; get; }

    [JsonProperty("key")]
    public string key { set; get; }

    [JsonProperty("item_order")]
    public int order { set; get; }

    [JsonProperty("required")]
    public bool isRequired { set; get; }

    [JsonProperty("enabled")]
    public bool isEnabled { set; get; }

    public CellCustom fieldObject { set; get;}


    public FormField()
    {
        fieldObject = CreateInstance() as CellCustom;
    }

    private object CreateInstance()
    {
        return Activator.CreateInstance(Type.GetType("Hura.Models.Cells." + inputtype));
    }

    public Cell createCell()
    {
        return fieldObject.createCell();
    }
}
公共类表单
{
[JsonProperty(“id”)]
公共int id{set;get;}
[JsonProperty(“所有权”)]
公共字符串标题{set;get;}
[JsonProperty(“主体”)]
公共字符串体{set;get;}
[JsonProperty(“部门id”)]
公共字符串department_id{set;get;}
[JsonProperty(“字段”)]
公共列表字段{set;get;}
}
公共类FormField
{
[JsonProperty(“id”)]
公共int id{set;get;}
[JsonProperty(“所有权”)]
公共字符串标签{set;get;}
[JsonProperty(“inputtype”)]
公共字符串输入类型{set;get;}
[JsonProperty(“密钥”)]
公共字符串密钥{set;get;}
[JsonProperty(“项目订单”)]
公共整数顺序{set;get;}
[JsonProperty(“必需”)]
需要公共布尔值{set;get;}
[JsonProperty(“已启用”)]
公共布尔值已启用{set;get;}
公共单元格自定义字段对象{set;get;}
公共表单字段()
{
fieldObject=CreateInstance()作为CellCustom;
}
私有对象CreateInstance()
{
返回Activator.CreateInstance(Type.GetType(“Hura.Models.Cells.”+inputtype));
}
公共单元格createCell()
{
返回fieldObject.createCell();
}
}
下面是反序列化代码

string str= @"{""id"": 17,""title"": ""testform"",""body"": ""null"",""department_id"": 5,""fields"": [{""id"": 28,""title"": ""null"",""inputtype"": ""text"",""key"": ""f1474532070512"",""item_order"": 1,""required"": true,""enabled"": true}]}";
Form tstfrm = JsonConvert.DeserializeObject<Form>(str);
MainPage = new FillForm(tstfrm); 
string str=@“{”“id”“:17”“title”“:”“testform”“body”“:”“null”“department\u id”“:5”“fields”“:[{”“id”“:28”“title”“:”“null”“inputtype”“:”“text”“key”“:”“f1474532070512”“item\u order”“:1”“required”“:true”“enabled”“:true}];
表单tstfrm=JsonConvert.DeserializeObject(str);
主页=新的填写表单(tstfrm);
但是当我运行这段代码时,它给出了错误
System.ArgumentNullException:值不能为null。参数名称:type
即使我的JSON对象中没有任何名为“type”的字段


我的代码中有什么问题,如何解决?

您需要确保当前执行的程序集或mscorlib.dll中存在类型
“Hura.Models.Cells.”+inputtype
。如果没有,则必须指定程序集名称。(见附件。)

在示例代码中,请求的类型名称是
Hura.Models.Cells.text
,它不存在,因此将
Activator.CreateInstance
的类型参数返回null


也许空检查就足够了,但这取决于您想如何处理这种情况。

编辑前的第一个答案是正确的,非常感谢!问题出在类名称空间中。我在
inputtype
变量值中错误地输入了一个错误,现在它工作正常。你救了我的命!!谢谢。为了正确反映您的情况,我对编辑进行了回滚。不客气!很高兴我能帮助你。:-)(如果我的回答有帮助,你当然也可以投赞成票。)