Using C# StringBuilder Class to access XRPTipBot data



In this tutorial you will learn how to get the names of the last 20 users who have sent you TIPs. In order for this approach to be effective, we are going to use  StringBuilder Class.

As the String object is immutable, and creating a new string object requires a new allocation of space, C# introduced StringBuilder, a dynamic object that can boost performance when concatenating many strings together in a loop.

Read more about StringBuilder Class 

Needless yo say, for this project I’m going to use https://www.newtonsoft.com/json

1
2
3
using System.IO;
using System.Net;
using Newtonsoft.Json;

Next step.

We need to generate C# classes from json https://www.xrptipbot.com/u:parisbydavid/n:twitter/f:json

where “parisbydavid” is my Twitter username and you have to replace it with your own username.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public class User
{
public string username { get; set; }
public string userid { get; set; }
public string network { get; set; }
public string last_login { get; set; }
public string tipbot_created { get; set; }
public string create_reason { get; set; }
public string public_destination_tag { get; set; }
public object rejecttips { get; set; }
public string disablenotifications { get; set; }
public object destination_wallet { get; set; }
}

public class Sent
{
public int count { get; set; }
public double amount { get; set; }
}

public class Received
{
public int count { get; set; }
public double amount { get; set; }
}

public class Tips
{
public Sent sent { get; set; }
public Received received { get; set; }
}

public class Deposits
{
public int amount { get; set; }
}

public class IlpDeposits
{
public double amount { get; set; }
}

public class Donations
{
public Deposits deposits { get; set; }
public IlpDeposits ilpDeposits { get; set; }
}

public class Balance
{
public double amount { get; set; }
}

public class Stats
{
public Tips tips { get; set; }
public Donations donations { get; set; }
public Balance balance { get; set; }
}

public class Received2
{
public string id { get; set; }
public string moment { get; set; }
public double amount { get; set; }
public string from_user { get; set; }
public string to_user { get; set; }
public string network { get; set; }
public string context { get; set; }
public string from_network { get; set; }
public string to_network { get; set; }
public string tipcontext { get; set; }
}

public class Sent2
{
public string id { get; set; }
public string moment { get; set; }
public double amount { get; set; }
public string from_user { get; set; }
public string to_user { get; set; }
public string network { get; set; }
public string context { get; set; }
public object from_network { get; set; }
public object to_network { get; set; }
public object tipcontext { get; set; }
}

public class Ilpdeposit
{
public string id { get; set; }
public string moment { get; set; }
public string drops { get; set; }
public string fee { get; set; }
}

public class History
{
public List<Received2> received { get; set; }
public List<Sent2> sent { get; set; }
public List<Ilpdeposit> ilpdeposits { get; set; }
}

public class RootObject
{
public string stats_uuid { get; set; }
public int timestamp { get; set; }
public User user { get; set; }
public Stats stats { get; set; }
public History history { get; set; }
}

For this step, we use HttpWebRequest to create a request for the above mentioned URL
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

1
2
3
4
5
6
string json = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
json = reader.ReadToEnd();
}

Now that our “json” string is ready , we can start parsing it using our favorite tool Newtonsoft.Json

1
RootObject dobj = JsonConvert.DeserializeObject<RootObject>(json);

How does the StringBuilder works in this case?

1
2
3
4
5
6
7
StringBuilder sb = new StringBuilder();
foreach (var item in dobj.history.received)
{
sb.AppendLine(item.from_user);
}
Console.WriteLine(sb);
Console.ReadLine();

That’s all.