簡體   English   中英

JsonConvert.DeserializeObject在ActionResult方法中返回null

[英]JsonConvert.DeserializeObject returns null in ActionResult Method

我有下一個要反序列化的Json文檔:

{
  "General": {
    "Items": [
      {
        "fId": "divisionID",
        "frmt": "Text"
      },
      {
        "fId": "wcctOwnerID",
        "frmt": "Text"
      },
      {
        "fId": "qreID",
        "frmt": "Text"
      }
    ]
  }
}

我有這個課程:

public class Item
{        
    [JsonProperty(PropertyName = "fId")]
    public string fId { get; set; }

    [JsonProperty(PropertyName = "frmt")]
    public string frmt { get; set; }
}


public class General
{        
    [JsonProperty(PropertyName = "Items")]
    public List<Item> Items { get; set; }
}

我正在嘗試使用此行反序列化:

using (StreamReader r = new StreamReader(HostingEnvironment.ApplicationPhysicalPath + @"\Utils\OptionsByDB.json"))
{
    var json = r.ReadToEnd();
    Utils.General items = JsonConvert.DeserializeObject<Utils.General>(json);                    
}

但它返回null。 我做錯了什么?

您的問題是您的JSON不是General對象。

是其中具有 General對象的對象:

您需要這樣的類聲明:

public class JsonObject{

     [JsonProperty(PropertyName = "General")]
     public General rootObject {get; set;}
}

然后使用:

var jsonConverted = JsonConvert.DeserializeObject<JsonObject>(json);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM