Json Formatting.Indented 使用 Web Api 控制器、ASP.NET MVC
Posted
技术标签:
【中文标题】Json Formatting.Indented 使用 Web Api 控制器、ASP.NET MVC【英文标题】:Json Formatting.Indented using Web Api Controller, 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'
【问题讨论】:
【参考方案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;
【讨论】:
以上是关于Json Formatting.Indented 使用 Web Api 控制器、ASP.NET MVC的主要内容,如果未能解决你的问题,请参考以下文章