【问题标题】:Json Formatting.Indented using Web Api Controller, ASP.NET MVCJson Formatting.Indented 使用 Web Api 控制器、ASP.NET MVC
【发布时间】:2016-05-20 23:46:43
【问题描述】:

我为名称、短文本、地理位置和图像信息生成一个 json 数据,这些数据是我从 web api 收集的。通过我的代码,我可以生成 JSON 数据 /api/wiki/name。但我无法格式化数据。当我想将它保存在一个文件中时,它存储在一行中。我尝试在返回结果中写入 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'

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api formatting jsonserializer


    【解决方案1】:

    这是因为你需要将一个 JsonSerializerSettings 对象传递给 Json

    试试这个:

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

    【讨论】:

      【解决方案2】:

      使用 Newtonsoft 缩进很简单

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

      【讨论】:

        猜你喜欢
        • 2013-04-30
        • 2018-06-18
        • 1970-01-01
        • 2012-09-04
        • 1970-01-01
        • 2021-08-13
        • 1970-01-01
        • 2013-04-14
        相关资源
        最近更新 更多