简体   繁体   English

在 C# 中接收 API 响应

[英]Receive API Response in c#

im trying to get a this value that i send with JSON string POST, when i receive the response in postman, there's value like status, id, etc that i not POST, while i do understand get the value from JSON string, i dont quite understand to get a receive API value like status and Name我试图获得一个我用 JSON 字符串 POST 发送的这个值,当我在邮递员中收到响应时,有状态、id 等值我没有 POST,虽然我明白从 JSON 字符串中获取值,但我不太明白理解获取像状态和名称这样的接收 API 值

邮递员结果

what i do tried recently我最近尝试过什么

    public class Condition
        {
            public string status { get; set; }
            public string name { get; set; }
            public string positition { get; set; }
            public int no_id { get; set; }
            public string time_auth { get; set; }
            public string account_type { get; set; }
         }

for the method对于方法

public partial class ResponseJSON
    {
        public bool Result(string jsonData, string URL, out string status)
        {
            bool resultresponse = false;
            var request = (HttpWebRequest)WebRequest.Create(URL);
            request.ContentType = "application/json";
            request.Method = "POST";

            var writer = new StreamWriter(request.GetRequestStream());
            string wrote = jsonData;
            writer.Write(wrote);

            var httpResponse = (HttpWebResponse)request.GetResponse();
            var streamReader = new StreamReader(httpResponse.GetResponseStream());
            var result = streamReader.ReadToEnd();

            dynamic R = JsonConvert.DeserializeObject<dynamic>(wrote);
            status = R.auth_type;

            return resultresponse;
        }

        
    }

and for the main和主要

string jsonData = @"{  
        'auth_type':'7',  
        'staff_id':'1234567890',
        'staff_pin': '1234'}";
        dynamic data = JObject.Parse(jsonData);
        string URL = "Link URL";
        string status = string.Empty;

        ResponseJSON responseJSON = new ResponseJSON();
        responseJSON.Result(jsonData, URL, out status);

您可能希望在客户端声明相同的类“条件”,因此您可以使用 Newtonsoft 反序列化响应,如下所示:

var conditionResponse = JsonConvert.DeserializeObject<Condition>(jsonData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM