【问题标题】:Nested Object in Json Post requestJson Post 请求中的嵌套对象
【发布时间】:2020-06-24 13:34:30
【问题描述】:

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

我做错了吗?有没有更好的方法?

【问题讨论】:

  • 您的 post 请求是什么样的 - 它期望什么类型的对象?

标签: c# json restsharp webapi


【解决方案1】:

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

Client.PostAsJsonAsync(url, obj);

它在 Microsoft.AspNet.WebApi.Client nuget 包中

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 2015-03-19
    • 1970-01-01
    • 2020-06-19
    • 2017-05-19
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多