Json Post 请求中的嵌套对象
Nested Object in Json Post request
我需要创建一个如下所示的 post 请求,其中包含一个嵌套对象。我不确定如何将 "Person": 部分添加到请求本身。我尝试过不同的东西,现在我想我完全想不通了。
这是我需要的:
{
"LocationId": 76349
"Date": 07/05/2020
"AppointmentType": "Xray / Casting"
"Person":{
"Lastname":"Smith","Firstname":"John","Gender":"M","Age":26}
}
And here's the last thing I've tried. This fails with "Unable to determine Json object type for type Person
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using System.IO;
using Newtonsoft.Json.Linq;
using Json.Net;
public class Person
{
public string Lastname { get; set; }
public string Firstname { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
}
JObject jObjectbody = new JObject();
jObjectbody.Add"LocationId", 76349);
jObjectbody.Add("Date", 07/05/2020);
jObjectbody.Add"AppointmentType", "Xray / Casting");
jObjectbody.Add(new Patient
{
Lastname = "Smith",
Firstname = "John",
Gender = "M",
Age = 26
});
我是不是做错了?有更好的方法吗?
您不需要创建一个 json,您可以像这样简单地传递对象。
Client.PostAsJsonAsync(url, obj);
它在 Microsoft.AspNet.WebApi.Client nuget 包中
我需要创建一个如下所示的 post 请求,其中包含一个嵌套对象。我不确定如何将 "Person": 部分添加到请求本身。我尝试过不同的东西,现在我想我完全想不通了。 这是我需要的:
{
"LocationId": 76349
"Date": 07/05/2020
"AppointmentType": "Xray / Casting"
"Person":{
"Lastname":"Smith","Firstname":"John","Gender":"M","Age":26}
}
And here's the last thing I've tried. This fails with "Unable to determine Json object type for type Person
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using System.IO;
using Newtonsoft.Json.Linq;
using Json.Net;
public class Person
{
public string Lastname { get; set; }
public string Firstname { get; set; }
public string Gender { get; set; }
public int Age { get; set; }
}
JObject jObjectbody = new JObject();
jObjectbody.Add"LocationId", 76349);
jObjectbody.Add("Date", 07/05/2020);
jObjectbody.Add"AppointmentType", "Xray / Casting");
jObjectbody.Add(new Patient
{
Lastname = "Smith",
Firstname = "John",
Gender = "M",
Age = 26
});
我是不是做错了?有更好的方法吗?
您不需要创建一个 json,您可以像这样简单地传递对象。
Client.PostAsJsonAsync(url, obj);
它在 Microsoft.AspNet.WebApi.Client nuget 包中