-1

I Would like to create a program in C# with Pushbullet API.

Now I receive my notifications from my phone but I would like to exploit data received.

How to analyse and extract data like this:

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

How can I read code like this?

Regex?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
C. MARTIN
  • 43
  • 12

1 Answers1

0

Here's a working .Net Fiddle => https://dotnetfiddle.net/9Lt67g

Details of the solution: Follow the following steps. The entire code is also pasted at the end for completeness (in case dotnetfiddle is down or 'dies' in the future)

STEP 1

Use json2csharp.com or something similar to generate C# POCOs from your JSON Response. Here's what I generated using the sample JSON you provided.

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; }
}

STEP 2

Deserialize the JSON into your POCOs using 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 :)

Here's the console output:

enter image description here

Complete Solution Code

(Note, you'll need to add Newtonsoft.Json to your Project using Nuget via Package Manager Console.)

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; }
}
Community
  • 1
  • 1
Shiva
  • 20,575
  • 14
  • 82
  • 112
  • Thx I try this when I've the time. But I had some problems when I used ClientWebSocket from System.Net, now I use WebSocketSharp because when I used ClientWebSocket some part of message missed. – C. MARTIN Dec 14 '16 at 02:15