System.Text.Json without knowing the response

359 views Asked by At

I'm getting used to using System.Text.Json and I am wondering if there is a way to parse a JSON Response without knowing the fields that will come down. As an example the code I have to get an access token is as such:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseUrl + resource);
        request.Method = WebRequestMethods.Http.Post;            
        request.Headers["Authorization"] = "Basic "+credentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(stream);
        var ddoc = JsonSerializer.Deserialize<JsonValues>(streamReader.ReadToEnd());
        bearerAuth = ddoc.Access_Token;

That's all well and good as the JsonValues class contains an Access_Token object. But is it possible to download and parse the JSON values without knowing the object names first and not have them in a class?

I'd still prefer to be able to access them through ddoc.XYZABC if possible?

0

There are 0 answers