Reputation:
I want to filter out all the sections of numbers of a string and add them all to a list of strings.
So from: ["2931741444","2931789497","2931745064","2931763896","2931728251","2931786984","2931799607","293177823","2931795568","293171105"]
to
list.Add(2931741444);
list.Add(2931789497);
list.Add(2931745064);
etc...
I loop through the string, look at where the numbers began and add them to a new string, but I'm stuck now. Here's my attempt:
static void Main(string[] args) {
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("http://api.hivemc.com/v1/game/SG/data");
Console.WriteLine(json);
Console.WriteLine("Was called first.");
int counter = 2;
string newString = "";
string s = json.ToString();
// loop through the string
for (int i=0; i < s.Length; i++)
{
if (s[i].ToString() == "\"")
{
counter++;
}
if (counter.isEven)
{
newString = newString + i;
}
}
}
Console.ReadKey();
}
Is there an easier way to do this? I also can't see how I would do the other strings with this code.
Upvotes: 1
Views: 1133
Reputation: 760
I think what you're trying to do is deserialization
. You can use Json.NET
var list = new List<long>();
using (var wc = new WebClient())
{
var json = wc.DownloadString("http://api.hivemc.com/v1/game/SG/data");
list = JsonConvert.DeserializeObject<List<long>>(json);
}
Upvotes: 1
Reputation: 1062640
var stringValues = JsonConvert.DeserializeObject<string[]>(json);
int[] values = Array.ConvertAll(stringValues, s => int.Parse(s));
?
(feel free to swap for any other json serializer library there; the example is with Json.NET)
Upvotes: 1