-1

I'm doing this program on c# with windows forms, the user will have a qr scanner and will have to click one button which will start a query.

The scanner from the qr reads informations like this

{
    "se": "NUMBERS",
    "de": "NUMBERS",
    "cs": "NUMBERS",
    "pc": "NUMBERS",
    "nm": "NUMBERS",
    "tp": "DEL",
    "dt": "NUMBERS",
    "tz": "UTC+01",
    "dk": ""
}

How do i extract the numbers from the qr string?

The qr all always the same, only the numbers will change, i need to extract the numbers from the string and use them later for sql query.

Peter Szekeli
  • 2,712
  • 3
  • 30
  • 44

1 Answers1

0

That looks like a JSON String so you could use the Newtonsoft.Json nuget package to parse the string and use it's results.

Here is the class you have to use for that specific string (this site is very handy)

public class AnObject
{
    public string se { get; set; }
    public string de { get; set; }
    public string cs { get; set; }
    public string pc { get; set; }
    public string nm { get; set; }
    public string tp { get; set; }
    public string dt { get; set; }
    public string tz { get; set; }
    public string dk { get; set; }
}

and then you can parse the string like so

public static void Main()
{
    var scannerInput = "{\"se\":\"NUMBERS\",\"de\":\"NUMBERS\",\"cs\":\"NUMBERS\",\"pc\":\"NUMBERS\",\"nm\":\"NUMBERS\",\"tp\":\"DEL\",\"dt\":\"NUMBERS\",\"tz\":\"UTC+01\",\"dk\":\"\"}";
    var obj = JsonConvert.DeserializeObject<AnObject>(scannerInput);


    Console.WriteLine(obj.se);
    Console.WriteLine(obj.de);
    Console.WriteLine(obj.cs);
    Console.WriteLine(obj.pc);
}

full example: https://dotnetfiddle.net/RTdMRF

note: as Tim said, if your string is always formatted like that and your numbers have no commas (also timezone and dk field should have no commas), his answer should give you an easier and faster solution. Also no extra packages.