C
C#15mo ago
Livid

✅ HttpClient to Json

hey i tried a number of ways to get json from response.Content.ReadAsStringAsync()
6 Replies
Livid
LividOP15mo ago
but i don't want to create the class to use by myself and no newtonsoft :(
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
SG97
SG9715mo ago
no System.Text.Json either? agree with Tebe, that's the only proper way
nkowalec
nkowalec15mo ago
You can use System.Text.Json to deserialization, or if you don't want DTO you can parse JsonDocument
string text = "{ \"prop1\": \"value1\" }";
var doc = JsonDocument.Parse(text);
var val = doc.RootElement.GetProperty("prop1").GetString();
string text = "{ \"prop1\": \"value1\" }";
var doc = JsonDocument.Parse(text);
var val = doc.RootElement.GetProperty("prop1").GetString();
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
Livid
LividOP15mo ago
finally i did
var result = await response.Content.ReadAsStringAsync();
JsonNode node = JsonNode.Parse(result);
... = node[0]["name"].ToString();
var result = await response.Content.ReadAsStringAsync();
JsonNode node = JsonNode.Parse(result);
... = node[0]["name"].ToString();

Did you find this page helpful?