我必须调用一个需要3级json请求的api。
class foo { public List<StudentList> students { get; set; } }
class StudentList
{
public string studentid { get; set; }
public string researchId { get; set; }
public List<courses> courseList { get; set; }
public List<examsschedule> exams { get; set }
}
class courses {
public string courseId { get; set; }
public string courseName { get; set; }
public string professsorName { get; set; }
}
class exams
{
/****/
}
我必须将此作为json发布到api。 postasjsonasynch
当我发送&#34; foo&#34;的对象时class,api拒绝,因为它不是json格式。
var payload = fooObject
/*object of foo*/
var httpContent = new StringContent(payload, Encoding.UTF8,"application/json");
我正在使用web api的postasjsonaynch
[jsonproperty]是仅用于捕获响应还是构建请求?
答案 0 :(得分:0)
这是解决方案,
static void Main(string[] args)
{
var url = "http://localhost:64743/";
var client = new HttpClient();
client.BaseAddress = new Uri(url);
//Post
var students = new List<StudentList>
{
new StudentList { researchid="1234", studentid="5689",
courses = new List<Course>
{
new Course { courseName="os", professorName="donchi lee" },
new Course { courseName="database", professorName="jie,joe" }
},
exams=new List<Exam> { }
}
};
var response = client.PostAsJsonAsync("api/Student", students).ConfigureAwait(false).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
}