I got this data returned from webhooks, but it's not a JSON object and can't use it currently. What kind of object is this and how can I Serialize this with C# or PHP?
{{
"data": "{\"event\":\"INBOX\",\"from\":\"966******\",\"to\":\"966******\",\"text\":\"https:\\/\\/fs.magicrepository.com\\/incoming\\/4ef5613331213ab08888fb7cf72f332c.jpg\",\"alias\":\"\",\"pushname\":\"\",\"profilepicture\":\"\"}"
}}
CodePudding user response:
Try this. Convert the data from webhook in json decoded form and then save it.
$decoded_data = json_decode($data, true);
CodePudding user response:
Assuming you want to deserialize the input:
- trim the excess curly braces
- Decode the result as JSON
- Decode the value of the
data
property as JSON.
INn C#, it would look like this:
using System.Text.Json;
/* assuming input is like:
* {{
* "data": "{\"event\":\"INBOX\",\"from\":\"966******\",\"to\":\"966******\",\"text\":\"https:\\/\\/fs.magicrepository.com\\/incoming\\/4ef5613331213ab08888fb7cf72f332c.jpg\",\"alias\":\"\",\"pushname\":\"\",\"profilepicture\":\"\"}"
* }}
*/
var unwrapped = input[1..(input.Length - 1)];
var outerDoc = JsonDocument.Parse(unwrapped).RootElement;
var innerDoc = JsonDocument.Parse(outerDoc.GetProperty("data").GetString()).RootElement;