Search code examples
c#asp.net-mvcasp.net-web-apiformattingjsonserializer

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


I generate a json data for name, shorttext, geolocation ann image information, which I collect from web api. Throug my code I can generate JSON data /api/wiki/name. But I cannot formatting the data. When I want to save it in a file it stores in a line.I tried to write Formatting.Indented in the return result, but it shows error. My code look like this-

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

The error I found is -

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'

Solution

  • It is because you need to pass a JsonSerializerSettings object to Json

    Try this:

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