簡體   English   中英

從httpclient的響應字符串中獲取鍵的值

[英]Grabbing value from a key from a response string from httpclient

我試圖使用System.Net.Http從發布請求中獲取值。 鍵以字典的字符串表示形式返回。

我的響應字符串看起來像這樣,字典的字符串表示形式:

"{\"primaryKey\": \"hereIsMyKeyValue\",\"secondaryKey\":\"jsfidjsi\"}"

相關代碼

這是我現在正在做的工作,以發出職位請求並閱讀我的回復

var response = await httpClient.PostAsync(url, content);

var responseString = await response.Content.ReadAsStringAsync();

我嘗試過的

我嘗試使用.Trim()擺脫轉義字符\\ 但這無能為力。

   var test = responseString.Trim();

如何獲取字符串表示形式中的那些鍵的內容? 還是我通過嘗試處理response.Content.ReadAsStringAsync()返回的內容來解決問題?

對您的嘗試做一些假設,

您可能想使用類似NewtonSoft的方法反序列化Json:

class MyDictionaryItem
{
    [JsonProperty("primaryKey")]
    public string PrimaryKey { get; set; }

    [JsonProperty("secondaryKey")]
    public string SecondaryKey { get; set; }
}

var myResult = JsonConvert.DeserializeObject<MyDictionaryItem>(responseString);

然后,您可以作為myResult成員訪問所需的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM