【问题标题】:How do I consume Api for C# project如何为 C# 项目使用 Api
【发布时间】:2022-01-07 19:41:46
【问题描述】:

我正在尝试为 c# 项目使用 Api。 我的请求正文示例如下:

{
   "tx_ref":"hooli-tx-1920bbtytty",
   "amount":"100",
   "currency":"NGN",
   "redirect_url":"https://webhook.site/9d0b00ba-9a69-44fa-a43d-a82c33c36fdc",
   "payment_options":"card",
   "meta":{
      "consumer_id":23,
      "consumer_mac":"92a3-912ba-1192a"
   },
   "customer":{
      "email":"user@gmail.com",
      "phonenumber":"080****4528",
      "name":"Yemi Desola"
   },
   "customizations":{
      "title":"Pied Piper Payments",
      "description":"Middleout isn't free. Pay the price",
      "logo":"https://assets.piedpiper.com/logo.png"
   }
}

我想将代码转换为 C#,我按照下面的方式进行了操作,但是当我尝试将代码的嵌套部分复制到 c# 时,请求中嵌套了“元”、“客户”、“自定义”我收到一个错误。

请问如何放置我的 C# 代码以反映带有代码嵌套部分的主体?

var keyValues = new Dictionary<string, string>
                            {

                                { "tx_ref", "N-872653uy09-9"},
                                { "amount", "500"},
                                { "currency","NGN"},
                                { "redirect_url","mysite.com/ConcludeFunding.aspx"},
                                { "payment_options","card"},
                                { "meta"," "},
                                { "customer"("email",EmailV)  },
                                { "customizations","mysite.com/Assets/Logo.jpg"},

                            };
            //serialization using Newtonsoft JSON 
            string JsonBody = JsonConvert.SerializeObject(keyValues);

【问题讨论】:

  • 为您的数据创建类。您可以使用这样的工具根据您的 JSON 自动生成 C# 类:json2csharp.com(最好使用“使用 Pascal Case”选项以保持属性命名的一致性)
  • 如果要使用字典,则需要为&lt;string, Object&gt;类型。然后为嵌套数据的值创建new Dictionary&lt;string, Object&gt; 对象
  • 或者使用 Visual Studio 粘贴 Json 作为类:c-sharpcorner.com/article/…
  • @OneCricketeer 请您举例说明如何为嵌套数据的值创建新的 Dictionary 对象
  • 你已经完成了一个对象的一层;多加几个有什么难的?例如,{ "meta", new Dictionary&lt;string, Object&gt; {"consumer_id", 23}},

标签: c# json api http


【解决方案1】:

对于这种情况,我会创建一个根类。然后我会为类对象赋值,最后我会使用 Newtonsoft JSON 序列化对象。

代码:

using Newtonsoft.Json;
using System;

namespace Solve
{
    internal class Program
    {
        public class Meta
        {
            public int consumer_id { get; set; }
            public string consumer_mac { get; set; }
        }

        public class Customer
        {
            public string email { get; set; }
            public string phonenumber { get; set; }
            public string name { get; set; }
        }

        public class Customizations
        {
            public string title { get; set; }
            public string description { get; set; }
            public string logo { get; set; }
        }

        public class Root
        {
            public string tx_ref { get; set; }
            public string amount { get; set; }
            public string currency { get; set; }
            public string redirect_url { get; set; }
            public string payment_options { get; set; }
            public Meta meta { get; set; }
            public Customer customer { get; set; }
            public Customizations customizations { get; set; }
        }
        static void Main(string[] args)
        {
            var rootObj = new Root()
            {
                tx_ref = "N-872653uy09-9",
                amount = "500",
                currency = "NGN",
                redirect_url = "mysite.com/ConcludeFunding.aspx",
                payment_options = "card",
                meta = new Meta()
                {
                    consumer_id = 23,
                    consumer_mac = "92a3-912ba-1192a"
                },
                customizations = new Customizations()
                {
                    title = "Pied Piper Payments",
                    description = "Middleout isn't free. Pay the price",
                    logo = "https://assets.piedpiper.com/logo.png"
                },
                customer = new Customer()
                {
                    email = "user@gmail.com",
                    phonenumber = "080****4528",
                    name = "Yemi Desola"
                }
            };
            string json = JsonConvert.SerializeObject(rootObj);
            Console.WriteLine(json);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2021-09-19
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多