C#格式化数组以发布到Api [英] C# format an array for post to Api

查看:56
本文介绍了C#格式化数组以发布到Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为传递给发布请求的json数组获取正确的格式.

I am trying to get the correct format for the json array I need to pass to a post request.

当使用c#作为首选编程语言时,我得到的示例代码显示以下格式:

The example code I got shows the following format when using c# as the preferred programming language:

var client = new RestClient("https://api.us/v2/webinars/94395753143/panelists");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", "Bearer _XU6l1eaDs9NQRTcb5QG4m0-ab1F3Y29ikw");
request.AddParameter("application/json", "{\"panelists\":[{\"name\":\"Mary\",\"email\":\"maryjkdfdsgfshdgf@jdfdkjdglfk.jkfgdj\"},{\"name\":\"Mike\",\"email\":\"dfdsgfsdhf@jkgfdgfkdhgfdjg.fkjgdf\"}]}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

作为概念证明,我是从angularjs视图中获取格式化json数组的值,并将其传递给post请求:

As proof of concept, I am getting the values from an angularjs view formatting the json array, and passing them through the post request:

$http(request)
    .then(function successCallback(data) {
    
        angular.forEach(data.data, function (item) {
            
            var b = {
                name: item.Name,
                email: item.Email
            };
            $scope.arr.push(b);         
        });

        var parData = JSON.stringify({ 'panelists': $scope.arr, 'id': $scope.webinarId, 'bearer':  $scope.bearer});

    $http.post('/api/AddPanelists', parData)
        .then(function (data) {
    }), function (data) {
        alert("An error occurred during the request");
    };

在C#代码中,我有一个函数,该函数接收parDAta并检索传递的3个元素(数组,id,载体)中的每一个

In my C# code I have a function that receives parDAta and retrieves each of the 3 elements passed (array, id, bearer)

    [HttpPost]
    public void CreatePanelists(Newtonsoft.Json.Linq.JObject data)
    {
        Parameters parameters = JsonConvert.DeserializeObject<Parameters>(data.ToString());

        IList<Panelist> panelists = parameters.panelists;  << this value does not seem right
        string webId = parameters.id;      << this value is fine
        string bearer = parameters.bearer; << this value is fine

        var client = new RestSharp.RestClient("https://api.us/v2/webinars/" + webId + "/panelists");
        var request = new RestRequest(Method.POST);
        request.AddHeader("content-type", "application/json;charset=UTF-8");
        request.AddHeader("authorization", "Bearer " + bearer);
        request.AddParameter("application/json", panelists, ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        var content = response.Content;

    }
    
    public class Panelist
    {
        public string name { get; set; }
        public string email { get; set; }
    }
    public class Parameters
    {
        public IList<Panelist> panelists { get; set; }
        public string id { get; set; }

        public string bearer { get; set; }
    }

当我在浏览器中检查console.log时,要查看JavaScript是否格式化json,我会看到:

When I check the console.log in the browser, to see if JavaScript is formatting the json is correct, I see this:

{"panelists":[{"name":"Jack Anderson","email":"janderson@email.com"},{"name":"Ed Johnson","email":"ejohnson@email.com"},{"name":"Dead Poole","email":"dpoole@email.com"},{"name":"Hank  Schmidt","email":"hschmidt@email.com"},{"name":"Steven Alves","email":"salves@email.com"},{"name":"Nilvio Alexander","email":"nalexanderemail.com"}],"id":94395753143,"bearer":"U19hW2pkQkO2A0Zv5EXz-h4kXJ56s"}

当我检查完它在C#端的值后,字符串看起来有点不同:

When I check the value once it makes it to the C# side of things, the string looks a bit different:

当我检查已通过的内容时,在这里:

When I check what has been passed, here:

public void CreatePanelists(Newtonsoft.Json.Linq.JObject data) << here

该值看起来:

{{
  "panelists": [
    {
      "name": "Jack Anderson",
      "email": "janderson@email.com"
    },
    {
      "name": "Ed Johnson",
      "email": "ejohnson@email.com"
    },
    {
      "name": "Dead Poole",
      "email": "dpoole@email.com"
    },
    {
      "name": "Hank  Schmidt",
      "email": "hschmidt@email.com"
    },
    {
      "name": "Steven Alves",
      "email": "salves@email.com"
    },
    {
      "name": "Nilvio Alexander",
      "email": "nalexander@email.com"
    }
  ],
  "id": 94395753143,
  "bearer": "U19hW2pkQkO2A0Zv5EXz-h4kXJ56s"
}}

我在字符串的开头和结尾处看到多余的花括号.

我的目标是能够传递一个看起来完全像第三方api需要格式化的数组的数组,我认为我做的不正确.

My goal is to be able to pass an array that looks exactly as the 3rd party api needs the array to be formatted, I do not think I am doing it correctly.

这是我需要的格式,专门用于 panelist 字符串

This is the format I need to have, specifically for the panelist string

{
  "panelists": [
    {
      "name": "Jack Anderson",
      "email": "janderson@email.com"
    },
    {
      "name": "Ed Johnson",
      "email": "ejohnson@email.com"
    },
    {
      "name": "Dead Poole",
      "email": "dpoole@email.com"
    },
    {
      "name": "Hank  Schmidt",
      "email": "hschmidt@email.com"
    },
    {
      "name": "Steven Alves",
      "email": "salves@email.com"
    },
    {
      "name": "Nilvio Alexander",
      "email": "nalexander@email.com"
    }
  ]
 }

感谢您阅读我的问题,希望我能有所帮助. 如果我需要澄清任何事情,请让我知道.

Thank you for reading my question, I hope I can get a bit of help. If I need to clarify anything, please just let me know.

谢谢, 伊拉斯莫(Erasmo)

Thanks, Erasmo

更新

我认为多余的花括号不见了,但是我怎么知道要传递的字符串看起来像这样:

I think the extra curly braces are gone, but how do I know that the string to be passed will look like this:

{
  "panelists": [
    {
      "name": "Jack Anderson",
      "email": "janderson@email.com"
    },
    {
      "name": "Ed Johnson",
      "email": "ejohnson@email.com"
    },
    {
      "name": "Dead Poole",
      "email": "dpoole@email.com"
    },
    {
      "name": "Hank  Schmidt",
      "email": "hschmidt@email.com"
    },
    {
      "name": "Steven Alves",
      "email": "salves@email.com"
    },
    {
      "name": "Alejandro Nava-Gomez",
      "email": "anava-gomez@cityofmadison.com"
    },
    {
      "name": "itortu",
      "email": "itortu@gmail.com"
    }
  ]
 }

如果您看到屏幕截图,则不确定它是否正确.可能是我正在查看输出,但不了解将传递什么实际值.

If you see the screen shot, I am not sure it looks correct. May be I am looking at the output and not understanding what the actual value will be passed.

推荐答案

您可以创建新的对象来满足预期的结构.

You can create new object to satisfy expected structure.

[HttpPost]
public void CreatePanelists([FromBody] Parameters parameters)
{
    var webId = parameters.id; 
    var bearer = parameters.bearer;
    var data = new
    {
        panelists = parameters.panelists
    };

    var client = 
        new RestSharp.RestClient($"https://api.us/v2/webinars/{webId}/panelists");
    var request = new RestRequest(Method.POST);
    request.AddHeader("content-type", "application/json;charset=UTF-8");
    request.AddHeader("authorization", $"Bearer {bearer}");

    request.AddParameter("application/json", data, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);
    var content = response.Content;
}

这篇关于C#格式化数组以发布到Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆