Json Formatting.Indented 使用 Web Api 控制器,ASP.NET MVC

Json Formatting.Indented using Web Api Controller, ASP.NET MVC

我为名称、短文本、地理位置和图像信息生成了一个 json 数据,这些数据是我从网络 api 收集的。通过我的代码,我可以生成 JSON 数据 /api/wiki/name。但我无法格式化数据。当我想将它保存在文件中时,它存储在 line.I 中,试图在 return 结果中写入 Formatting.Indented,但它显示错误。我的代码看起来像这样-

 public class WikiController : ApiController
{
    public JsonResult<PoiInfo> Get(string id)
    {
        WebClient client = new WebClient();
        var TextResponse = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=" + id + "&redirects=");
        var ImageResponse = client.DownloadString(new Uri("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=pageimages&pithumbsize=400&titles=" + id));
        var GeoResponse = client.DownloadString("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=coordinates&titles=" + id);


        var TextResponseJson = JsonConvert.DeserializeObject<Rootobject>(TextResponse);
        var TextfirstKey = TextResponseJson.query.pages.First().Key;
        var Text = TextResponseJson.query.pages[TextfirstKey].extract;
        Regex regex = new Regex(@".(?<=\()[^()]*(?=\)).(.)");
        string.Format("Before:{0}", Text);
        Text = regex.Replace(Text, string.Empty);
        string TextResult = String.Format(Text);
        TextResult = Regex.Replace(TextResult, @"\n", " ");

        var ImgresponseJson = JsonConvert.DeserializeObject<ImgRootobject>(ImageResponse);
        var ImgfirstKey = ImgresponseJson.query.pages.First().Key;
        var ImageResult = ImgresponseJson.query.pages[ImgfirstKey].thumbnail.source;

        var GeoResponseJson = JsonConvert.DeserializeObject<GeoRootobject>(GeoResponse);
        var firstKey = GeoResponseJson.query.pages.First().Key;
        var Latitude = GeoResponseJson.query.pages[firstKey].coordinates.First().lat;
        var Longitude = GeoResponseJson.query.pages[firstKey].coordinates.First().lon;

        var result = new PoiInfo();
        result.Shorttext = Text;
        result.Name = id;
        result.Images = new List<string> { ImageResult };
        result.GeoCoordinates = new GeoCoordinates
        {
            Latitude = Latitude,
            Longitude = Longitude
        };

        return Json(result,Formatting.Indented); //show error
    }
}

我发现的错误是-

Error 1 The best overloaded method match for   'System.Web.Http.ApiController.Json<CallListService.Models.PoiInfo>(  (CallListService.Models.PoiInfo, Newtonsoft.Json.JsonSerializerSettings)' has some invalid arguments

 Error  2   Argument 2: cannot convert from 'Newtonsoft.Json.Formatting' to 'Newtonsoft.Json.JsonSerializerSettings'

因为需要传递一个JsonSerializerSettings对象给Json

试试这个:

JsonSerializerSettings serializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented };        
return Json(result, serializerSettings);

使用 Newtonsoft Indent 很简单

private void ConfigureWebApi(HttpConfiguration httpConfig)
    {
        httpConfig.MapHttpAttributeRoutes();
        var jsonFormatter = httpConfig.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
    }