提问者:小点点

Json Post请求中的嵌套对象


我需要创建一个类似于下面的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        
            });    

我做错了吗? 有没有更好的办法做这件事?


共1个答案

匿名用户

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

Client.PostAsJsonAsync(url, obj);

它在Microsoft.aspnet.webapi.client nuget包中