0

I have below stream which I get from this line where req1 is of HttpResponseMessage type and responseMessage is of type Stream. How can I convert this Stream into a json Object. My end goal is to extract values from the specific keys in this json.

var responseMessage = await req1.Content.ReadAsStreamAsync();
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54
  • 1
    https://stackoverflow.com/a/22689976/9423721 – Markus Dresch Jan 11 '22 at 13:22
  • Does this answer your question? [Can Json.NET serialize / deserialize to / from a stream?](https://stackoverflow.com/questions/8157636/can-json-net-serialize-deserialize-to-from-a-stream) – Charlieface Jan 11 '22 at 20:31

2 Answers2

1

Above answer has a class defined. I didnt want to define different class as my model is dynamic. I found this solution , which worked well and got me the desired result

var serializer = new JsonSerializer();

            using (var sr = new StreamReader(responseMessage))
            using (var jsonTextReader = new JsonTextReader(sr))
            {
                var jsObj= serializer.Deserialize(jsonTextReader);
            }
ZZZSharePoint
  • 1,163
  • 1
  • 19
  • 54
-1

try it

// read file into a string and deserialize JSON to a type
Movie movie1 = JsonConvert.DeserializeObject<Movie>(File.ReadAllText(@"c:\movie.json"));

// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"c:\movie.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    Movie movie2 = (Movie)serializer.Deserialize(file, typeof(Movie));
}