Json Post请求中的嵌套对象

hrysbysz  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(171)

我需要创建一个如下所示的发布请求,请求中包含一个嵌套对象。我不知道如何添加“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        
            });

我是不是搞错了?有没有更好的办法?

axr492tv

axr492tv1#

你不需要创建一个json,你可以简单地像这样传递对象。

Client.PostAsJsonAsync(url, obj);

它位于Microsoft.aspNet.WebApi.客户端nuget包中

相关问题