Parse vcard json C#

别说谁变了你拦得住时间么 提交于 2019-12-22 09:25:37

问题


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.


回答1:


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..



来源:https://stackoverflow.com/questions/26278659/parse-vcard-json-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!