Home > Software design >  How to convert HTTP Response to JSON in Blazor
How to convert HTTP Response to JSON in Blazor

Time:02-14

I am trying to convert an HTTP response with the following format:

[
  {
    ID: 1
    ...
  },
  {
    ID: 2
    ...
  }
]

Into JSON so it looks like this:

{
  "data": [
    {
      ID: 1
      ...
    },
    {
      ID: 2
      ...
    }
  ]
}

Anyone know how I can do this in C# (so that it can be deserialized by a Blazor application)?

There isn't any way to add an argument to the HTTP request to coerce the response into JSON format, is there?

CodePudding user response:

try this

var json = await response.Content.ReadAsStringAsync();
json="{\n data:" json "\n}";

output

{
 data:[
  {
    "ID": 1
  },
  {
    "ID": 2
  }
]
}
  • Related