Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse vcard json C#

Tags:

json

c#

vcf-vcard

I want to parse the vcard RFC 7095 using Json.NET :

["vcard",
     [
       ["version", {}, "text", "4.0"],
       ["fn", {}, "text", "John Doe"],
       ["gender", {}, "text", "M"],
       ["categories", {}, "text", "computers", "cameras"],
       ...
     ]
   ]

I try to do it using FormatTypeFormater but I cannot validate the json.

like image 473
brmd Avatar asked Apr 14 '26 21:04

brmd


1 Answers

You can parse it using JavaScriptSerializer to a object[], then work on it to build a better complex type:

 var js = new JavaScriptSerializer();
 var o = (object[])js.Deserialize(@"[""vcard"",
   [
     [""version"", {}, ""text"", ""4.0""],
     [""fn"", {}, ""text"", ""John Doe""],
     [""gender"", {}, ""text"", ""M""],
     [""categories"", {}, ""text"", ""computers"", ""cameras""]
   ]
 ]", typeof(object[]));



if (o.length > 1 && (o[0] as string) == "vcard")
{
    var props = o[1] as object[];

    foreach (object[] values in props)
    {
        switch (values[0] as string)
        {
            case "version":
                ...
                break;
            case "fn":
                ...
                break;
            ....
        }
    }
}

You should implmenet more validation on this, but this is a good start..

like image 55
Luizgrs Avatar answered Apr 16 '26 09:04

Luizgrs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!