8

特定のクラスのオブジェクトに対してJsonResultを返すアクションがあります。nullフィールドを避けるために、このクラスのプロパティをいくつかの属性で装飾しました。クラス定義は次のとおりです。

    private class GanttEvent
    {
        public String name { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public String desc { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public List<GanttValue> values { get; set; }
    }

そして私のアクションでは、オブジェクトを使用します

    var res = new List<GanttEvent>();

私が使用して返すもの:

    return Json(res, JsonRequestBehavior.AllowGet);

残念ながら、出力時にまだnull値を受け取っています。

    [{"name":"1.1 PREVIOS AL INICIO ","desc":null,"values":null},{"name":"F04-PGA-S10","desc":"Acta preconstrucción","values":null},{"name":"F37-PGA-S10","desc":"Plan de inversión del anticipo","values":null},{"name":"F09-PGA-S10","desc":"Acta de vecindad","values":null},{"name":"F05-PGA-S10","desc":"Acta de inicio","values":null},{"name":"F01-PGA-S10","desc":"Desembolso de anticipo","values":null}]

私は何かを逃しているのですか、それとも何か間違っているのですか?

4

3 に答える 3

8

Brad Christie が述べたように、MVC4 はまだ JavaScriptSerializer を使用しているため、オブジェクトを Json.Net でシリアル化するには、いくつかの手順を実行する必要があります。

まず、次のように JsonResult から新しいクラス JsonNetResult を継承します (このソリューションに基づく )。

public class JsonNetResult : JsonResult
{
    public JsonNetResult()
    {
        this.ContentType = "application/json";
    }

    public JsonNetResult(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior jsonRequestBehavior)
    {
        this.ContentEncoding = contentEncoding;
        this.ContentType = !string.IsNullOrWhiteSpace(contentType) ? contentType : "application/json";
        this.Data = data;
        this.JsonRequestBehavior = jsonRequestBehavior;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        var response = context.HttpContext.Response;

        response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json";

        if (ContentEncoding != null)
            response.ContentEncoding = ContentEncoding;

        if (Data == null)
            return;

        // If you need special handling, you can call another form of SerializeObject below
        var serializedObject = JsonConvert.SerializeObject(Data, Formatting.None);
        response.Write(serializedObject);
    }
}

次に、コントローラーで Json メソッドをオーバーライドして、新しいクラスを使用します。

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonNetResult(data, contentType, contentEncoding, behavior);
}
于 2012-11-09T09:35:45.307 に答える
1

Controller.JsonNewtonsoft Json ライブラリJavaScriptSerializerではなくを使用します(これが元の場所です)。JsonPropertyAttribute

Newtonsoft ライブラリ ライブラリを使用してその方法でシリアル化された結果を返すか、呼び出しを続けて null を無視するコンバーターJsonを作成する必要があります。

于 2012-11-08T20:09:37.897 に答える
0

私の提案は、1 つの GanttEvent オブジェクトを JSON にシリアル化するとどうなるかを確認することです。また、Json への呼び出しが適切であることを確認してください。

于 2012-11-08T20:07:46.197 に答える