Pushbullet API-уведомление
Я хотел бы создать программу на C# с Pushbullet API.
Теперь я получаю уведомления с телефона, но я хотел бы использовать полученные данные.
Как анализировать и извлекать данные, как это:
{
"push": {
"application_name": "Pushbullet",
"body": "If you see this on your computer, Android-to-PC notifications are working!\n",
"client_version": 125,
"dismissable": true,
"has_root": false,
"icon": "(base64_encoded_jpeg)",
"notification_id": "-8",
"notification_tag": null,
"package_name": "com.pushbullet.android",
"source_device_iden": "ujpah72o0sjAoRtnM0jc",
"source_user_iden": "ujpah72o0",
"title": "Mirroring test",
"type": "mirror"
},
"type": "push"
}
Как я могу прочитать код, как это?
Regex?
1 ответ
Вот рабочая.Net Fiddle => https://dotnetfiddle.net/9Lt67g
Детали решения: выполните следующие шаги. Весь код также вставляется в конце для полноты (в случае, если dotnetfiddle не работает или "умрет" в будущем)
ШАГ 1
Используйте http://json2csharp.com/ или что-то подобное для создания POCO C# из вашего ответа JSON. Вот что я сгенерировал, используя предоставленный вами пример JSON.
public class Push
{
public string application_name { get; set; }
public string body { get; set; }
public int client_version { get; set; }
public bool dismissable { get; set; }
public bool has_root { get; set; }
public string icon { get; set; }
public string notification_id { get; set; }
public object notification_tag { get; set; }
public string package_name { get; set; }
public string source_device_iden { get; set; }
public string source_user_iden { get; set; }
public string title { get; set; }
public string type { get; set; }
}
public class RootObject
{
public Push push { get; set; }
public string type { get; set; }
}
ШАГ 2
Десериализовать JSON в ваши POCO с помощью JSON.Net
Notification notification = JsonConvert.DeserializeObject<Notification>(json);
Console.WriteLine("PushBullet API Notification...");
Console.WriteLine(" App Name: {0}", notification.push.application_name);
Console.WriteLine(" Notification Body: {0}", notification.push.body);
// .... etc. you get the idea. You now have the notification in a POCO and do
// whatever you want with it :)
Вот вывод консоли:
Полный код решения
(Обратите внимание, вам нужно добавить Newtonsoft.Json
в ваш проект с помощью Nuget через диспетчер пакетов.)
using System;
using Newtonsoft.Json;
public class Program
{
// Stack Overflow Question Link: http://stackru.com/q/41078332/325521
// My Answer Link: http://stackru.com/a/41113725/325521
// Author: Shiva Kumar
// Web: shiva.io
public static void Main()
{
var json = @"
{
""push"": {
""application_name"": ""Pushbullet"",
""body"": ""If you see this on your computer, Android-to-PC notifications are working!\n"",
""client_version"": 125,
""dismissable"": true,
""has_root"": false,
""icon"": ""(base64_encoded_jpeg)"",
""notification_id"": ""-8"",
""notification_tag"": null,
""package_name"": ""com.pushbullet.android"",
""source_device_iden"": ""ujpah72o0sjAoRtnM0jc"",
""source_user_iden"": ""ujpah72o0"",
""title"": ""Mirroring test"",
""type"": ""mirror""
},
""type"": ""push""
}";
Notification notification = JsonConvert.DeserializeObject<Notification>(json);
Console.WriteLine("PushBullet API Notification...");
Console.WriteLine(" App Name: {0}", notification.push.application_name);
Console.WriteLine(" Notification Body: {0}", notification.push.body);
// .... etc. you get the idea. You now have the notification in a POCO and do
// whatever you want with it :)
}
}
public class Push
{
public string application_name { get; set; }
public string body { get; set; }
public int client_version { get; set; }
public bool dismissable { get; set; }
public bool has_root { get; set; }
public string icon { get; set; }
public string notification_id { get; set; }
public object notification_tag { get; set; }
public string package_name { get; set; }
public string source_device_iden { get; set; }
public string source_user_iden { get; set; }
public string title { get; set; }
public string type { get; set; }
}
public class Notification
{
public Push push { get; set; }
public string type { get; set; }
}