使用ASP.NET的不正确的JSON输出

时间:2019-01-31 13:32:33

标签: c# asp.net json

请原谅这个问题,以示沮丧。虽然我知道问题的根源是将数据转换为不同的类型,但我不能专心致意导致此Web服务的get方法的结果输出错误,该方法包含反斜杠。

下面的代码总结了我方法的功能。我在这些论坛上尝试了不同的建议,其中一些是对我问自己类似问题的答复。

数据模型

public class WeatherResponse
{

    public class Coord
    {
        public double lon { get; set; }
        public double lat { get; set; }
    }

    public class Weather
    {
        public int id { get; set; }
        public string main { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
    }

    public class Main
    {
        public double temp { get; set; }
        public int pressure { get; set; }
        public int humidity { get; set; }
        public int temp_min { get; set; }
        public int temp_max { get; set; }
    }

    public class Wind
    {
        public double speed { get; set; }
        public int deg { get; set; }
    }

    public class Clouds
    {
        public int all { get; set; }
    }

    public class Sys
    {
        public int type { get; set; }
        public int id { get; set; }
        public double message { get; set; }
        public string country { get; set; }
        public int sunrise { get; set; }
        public int sunset { get; set; }
    }

    public class RootObject
    {
        public Coord coord { get; set; }
        public List<WeatherResponse> weather { get; set; }
        public string @base { get; set; }
        public Main main { get; set; }
        public int visibility { get; set; }
        public Wind wind { get; set; }
        public Clouds clouds { get; set; }
        public int dt { get; set; }
        public Sys sys { get; set; }
        public int id { get; set; }
        public string name { get; set; }
        public int cod { get; set; }
    }
}

控制器

    [HttpGet]
    public string Get(string city, string country)
    {

        string apiKey = "KEY";
        HttpWebRequest apiRequest = WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + country + " &appid=" + apiKey + "&units=metric") as HttpWebRequest;

        string apiResponse = "";
        using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            apiResponse = reader.ReadToEnd();
        }

        Response.ContentType = "application/json";

        return apiResponse;
    }

结果

  

“ {\” coord \“:{\” lon \“:-0.13,\” lat \“:51.51},\” weather \“:[{\” id \“:521,\” main \ “:\”雨水“,\”描述\“:\”淋浴   rain \“,\” icon \“:\” 09d \“}]],\” base \“:\” stations“,\” main \“:{\” temp \“:2.62,\” pressure \“ :991,\“湿度\”:69,\“ temp_min \”:1,\“ temp_max \”:4},\“可见度\”:10000,\“风\”:{\“速度\”:5.1 ,\“ deg \”:90},\“ clouds \”:{\“ all \”:75},\“ dt \”:1548939000,\“ sys \”:{\“ type \”:1,\ “ id \”:1414,\“ message \”:0.0037,\“ country \”:\“ GB \”,\“ sunrise \”:1548920401,\“ sunset \”:1548953318},\“ id \”: 2643743,\“名称\”:\“伦敦\”,\“鳕鱼\”:200}“

我需要使用接收到的城市和国家/地区来简单地检索天气数据并将其正确输出为JSON的代码。

1 个答案:

答案 0 :(得分:1)

这是解决问题的方法,如Joroen Mostert所贴。

  

您应该能够为此使用Content(更改退货时   您的方法类型为IActionResult):返回Content(apiResponse,   “ application / json”)不应再转义。