0

How can I read such outputs in C# perhaps converting to C# native lists? Without parse the text and split and make string manipulations, must have an easy way

[
    "\/recordings\/series\/seasons\/432250",
    "\/recordings\/series\/seasons\/263560"
]

I'm interested in just the numbers, I know that I could easily just use a string split using ',' and get the last 6 numerics chars

another example:

[
    "\/recordings\/series\/episodes\/428389",
    "\/recordings\/series\/episodes\/428386",
    "\/recordings\/movies\/airings\/434062",
    "\/recordings\/series\/episodes\/430801"
]

In that case will be interesting to know the <> paths typical json that I'm using to deserialize doesn't work with those dictionary likes json strings

thanks

Niyoko
  • 7,512
  • 4
  • 32
  • 59
AMoraes
  • 13
  • 4

3 Answers3

0

Try code below.

        var json = @"[
""\/recordings\/series\/episodes\/428389"",
""\/recordings\/series\/episodes\/428386"",
""\/recordings\/movies\/airings\/434062"",
""\/recordings\/series\/episodes\/430801""
]";

        var jarray = JArray.Parse(json);
        var list = from a in jarray
            let val = a.Value<string>()
            let rgroup = Regex.Match(val, @"[^0-9]*([0-9]+)")
            let vstring = rgroup.Groups[1]
            select int.Parse(vstring.Value);

It requires Newtonsoft.Json library, and list has type of IEnumerable<int>. And also don't forget to add

using System;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;

To the top of your .cs file.

Niyoko
  • 7,512
  • 4
  • 32
  • 59
0

Many people like to use the Json.NET / Newtonsoft.Json library. You can easily add it as a NuGet package to your project and then use it like this:

var json = "[\"/recordings/series/seasons/432250\",\"/recordings/series/seasons/263560\"]";

var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(json);

For the numbers, there are lots of ways to go about it. Here's one of many:

var numbersList = list.Select(x => x.Split('/').Last()).ToList();
UtopiaLtd
  • 2,520
  • 1
  • 30
  • 46
  • And in case you may be unsure as to how to add Json.NET to your project, this question covers that: http://stackoverflow.com/questions/4444903/how-to-install-json-net-using-nuget – UtopiaLtd Nov 03 '16 at 03:56
  • all the answers seems to solve the problem but I did test this one first since is just about one line of code ;-) I liked... that's enough for my needs and easy as expected Thanks – AMoraes Nov 04 '16 at 01:36
0

A simple way is to use JavaScriptSerializer from System.Web.Extensions.dll. Its Deserialize<T>() method lets you specify the type that the JSON should be deserialized to. In the case of simple primitives, such as an array of strings, it works magically without any fuss. An example:

var json = "[\"\\/recordings\\/series\\/seasons\\/432250\",\"\\/recordings\\/series\\/seasons\\/263560\"]";
var stringArray = new JavaScriptSerializer().Deserialize<string[]>(json);
foreach (var element in stringArray)
    Console.WriteLine(element.Split('/').Last());
binki
  • 7,754
  • 5
  • 64
  • 110
  • My programming skills are old as pascal, vb and C# and mostly for windows forms.. just now I'm finding some use for web programming and looks like Json is very popular ... glad to learn more.. thanks for the help – AMoraes Nov 04 '16 at 01:41