2016-10-08 7 views
1

nullにすることはできません私はここにTYPR FormDeserializeObjectエラー:値が

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(); 
    } 
} 

のオブジェクトをシリアライズとデシリアライズのための私のxamarin.formsアプリケーションにプラグインNewtonsoft.Jsonを使用しているデシリアライズコードは

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); 
です

しかし、このコードを実行すると、私のJSONオブジェクトに "type"という名前のフィールドがないのに、エラーSystem.ArgumentNullException: Value cannot be null. Parameter name: typeが返されます!

私のコードの問題は何ですか?どのように解決できますか?

答えて

1

"Hura.Models.Cells." + inputtypeが現在実行中のアセンブリまたはmscorlib.dllに存在することを確認する必要があります。そうでない場合は、アセンブリ名を指定する必要があります。 (hereを参照してください)

例コードでは、要求された型名はHura.Models.Cells.textであり、存在しないため、null型がの型パラメーターに戻されます。

おそらくヌルチェックで十分でしょうが、そのような状況にどのように対処したいかによって異なります。

+0

編集前の最初の回答が正解でした。ありがとうございます。問題はクラスの名前空間にありました。私は間違って 'inputtype'変数値でタイプミスをしましたが、今はうまくいきます。あなたは私のライブを救った!ありがとう。 –

+1

あなたの状況を正しく反映するように編集をロールバックしました。いいえ、どいたしまして!私はあなたを助けることができてうれしい。 :-)(もし私の答えが役に立つのであれば、あなたももちろんそれをupvoteすることができます。) – haindl

関連する問題