Parse vcard json c #

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

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

      

I am trying to do it using FormatTypeFormater, but I cannot check the json.

+3


source to share


1 answer


You can parse it with JavaScriptSerializer

do object[]

and then work on it to create a more 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 implement more validation, but this is a good start.

+2


source







All Articles