【问题标题】:Pushbullet API NotificationPushbullet API 通知
【发布时间】:2016-12-10 17:35:47
【问题描述】:

我想用 Pushbullet API 在 C# 中创建一个程序。

现在我从手机收到通知,但我想利用收到的数据。

如何分析和提取这样的数据:

{
  "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"
}

如何阅读这样的代码?

正则表达式?

【问题讨论】:

  • 例如,如果我有一个 push 元素,在“push”之后的 { } 之间读取值内容:{ 如果我有更多“push”:{ 我想将它分开。
  • “利用”?你这是什么意思?
  • 使用它(对不起,我是法国人。)
  • 现在我知道是json格式但我从来没有使用过反序列化功能(我也没有使用过序列化)

标签: c# api pushbullet


【解决方案1】:

这是一个有效的 .Net Fiddle => https://dotnetfiddle.net/9Lt67g

解决方案详情:按照以下步骤操作。为了完整起见,整个代码也粘贴在末尾(以防 dotnetfiddle 出现故障或将来“死亡”)

步骤 1

使用json2csharp.com 或类似的东西从您的 JSON 响应中生成 C# POCO。这是我使用您提供的示例 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.Net 将 JSON 反序列化为您的 POCO

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 :)

这是控制台输出:

完整的解决方案代码

(注意,您需要通过包管理器控制台使用 Nuget 将 Newtonsoft.Json 添加到您的项目中。)

using System;
using Newtonsoft.Json;
                    
public class Program
{
    // Stack Overflow Question Link: http://stackoverflow.com/q/41078332/325521
    // My Answer Link: http://stackoverflow.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; }
}

【讨论】:

  • 谢谢我有空的时候试试这个。但是我在使用 System.Net 中的 ClientWebSocket 时遇到了一些问题,现在我使用 WebSocketSharp,因为当我使用 ClientWebSocket 时,部分消息丢失了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
相关资源
最近更新 更多