我是ASP.NET的新手。最近,我创建了自己的api。因此,我决定创建一个Web表单来测试api。但是,响应没有显示出来,我相信我已正确完成了所有操作。
我在网络表单上的代码
using System;
using Newtonsoft.Json;
using FoodBlog.Model;
namespace FoodBlog
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var weatherData = new WeatherData();
var webClient = new System.Net.WebClient();
var json = webClient.DownloadString("https://1ibewdli19.execute-api.us-east-2.amazonaws.com/Working/blogid/1");
string replacedString = json.Replace("<", "");
string replacedString1 = replacedString.Replace(">", "");
WeatherData wdata = JsonConvert.DeserializeObject<WeatherData>(replacedString1);
Label1.Text = wdata.description;
}
}
}
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FoodBlog.Model
{
public class WeatherData
{
public string description { get; set; }
public string iconDesc { get; set; }
public string TimeStamp { get; set; }
}
}
JSON数据运行正常(使用邮递员)
{
"id": "<Humid and mostly cloudy throughout the day.>",
"icon": "<partly-cloudy-day>",
"time": "<1532448000>"
}
答案 0 :(得分:2)
您的JSON字符串与您的model
不匹配,您可以尝试使用JsonProperty属性映射您的JSON字符串和模型属性。
public class WeatherData
{
[JsonProperty("id")]
public string description { get; set; }
[JsonProperty("icon")]
public string iconDesc { get; set; }
[JsonProperty("time")]
public string TimeStamp { get; set; }
}
或使用此类,您将获取JSON数据。
public class WeatherData
{
public string id { get; set; }
public string icon { get; set; }
public string time { get; set; }
}