簡體   English   中英

在 C# 中接收 API 響應

[英]Receive API Response in c#

我試圖獲得一個我用 JSON 字符串 POST 發送的這個值,當我在郵遞員中收到響應時,有狀態、id 等值我沒有 POST,雖然我明白從 JSON 字符串中獲取值,但我不太明白理解獲取像狀態和名稱這樣的接收 API 值

郵遞員結果

我最近嘗試過什么

    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; }
         }

對於方法

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;
        }

        
    }

和主要

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