Ricerca…


Formattazione JSON predefinita: efficienza al costo della leggibilità

Diciamo che hai un semplice ApiController come questo:

    [HttpGet]
    [Route("test")]
    public dynamic Test()
    {
        dynamic obj = new ExpandoObject();
        obj.prop1 = "some string";
        obj.prop2 = 11;
        obj.prop3 = "another string";

        return obj;
    }

La rappresentazione JSON risultante di questo oggetto sarà simile a questa:

{"prop1":"some string","prop2":11,"prop3":"another string"}

Questo probabilmente va bene per risposte semplici come questa, ma immagina se hai un oggetto grande / complesso inviato come risposta:

"response": { "version": "0.1", "termsofService": "http://www.wunderground.com/weather/api/d/terms.html", "features": { "history": 1 } }, "history": { "date": { "pretty": "July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "12", "min": "00", "tzname": "America/Indianapolis" }, "utcdate": { "pretty": "July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "16", "min": "00", "tzname": "UTC" }, "observations": [{ "date": { "pretty": "12:15 AM EDT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "00", "min": "15", "tzname": "America/Indianapolis" }, "utcdate": { "pretty": "4:15 AM GMT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "04", "min": "15", "tzname": "UTC" }, "tempm": "18.2", "tempi": "64.8", "dewptm": "16.4", "dewpti": "61.5", "hum": "89", "wspdm": "9.3", "wspdi": "5.8", "wgustm": "-9999.0", "wgusti": "-9999.0", "wdird": "20", "wdire": "NNE", "vism": "16.1", "visi": "10.0", "pressurem": "1018.2", "pressurei": "30.07", "windchillm": "-999", "windchilli": "-999", "heatindexm": "-9999", "heatindexi": "-9999", "precipm": "-9999.00", "precipi": "-9999.00", "conds": "Clear", "icon": "clear", "fog": "0", "rain": "0", "snow": "0", "hail": "0", "thunder": "0", "tornado": "0", "metar": "METAR KTYQ 160415Z AUTO 02005KT 10SM CLR 18/16 A3007 RMK AO2 T01820164" }, { "date": { "pretty": "12:35 AM EDT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "00", "min": "35", "tzname": "America/Indianapolis" }, "utcdate": { "pretty": "4:35 AM GMT on July 16, 2016", "year": "2016", "mon": "07", "mday": "16", "hour": "04", "min": "35", "tzname": "UTC" }, "tempm": "17.7", "tempi": "63.9", "dewptm": "16.3", "dewpti": "61.3", "hum": "91", "wspdm": "7.4", "wspdi": "4.6", "wgustm": "-9999.0", "wgusti": "-9999.0", "wdird": "10", "wdire": "North", "vism": "16.1", "visi": "10.0", "pressurem": "1018.2", "pressurei": "30.07", "windchillm": "-999", "windchilli": "-999", "heatindexm": "-9999", "heatindexi": "-9999", "precipm": "-9999.00", "precipi": "-9999.00", "conds": "Clear", "icon": "clear", "fog": "0", "rain": "0", "snow": "0", "hail": "0", "thunder": "0", "tornado": "0", "metar": "METAR KTYQ 160435Z AUTO 01004KT 10SM CLR 18/16 A3007 RMK AO2 T01770163" } } }

Questo non è ciò che considereresti dati altamente leggibili. Questo è facilmente risolvibile impostando l'impostazione di una singola proprietà sul JsonFormatter predefinito in App_Start / ApiConfig.cs:

    // Either one of these will format your JSON in a readable format.  Setting both provides no additional benefit.
    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    // OR
    config.Formatters.JsonFormatter.Indent = true;


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow