解析vcard json C#

时间:2014-10-09 12:39:09

标签: c# json vcard

我想使用Json.NET解析vcard RFC 7095:

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

我尝试使用FormatTypeFormater来做,但我无法验证json。

1 个答案:

答案 0 :(得分:2)

您可以使用JavaScriptSerializer将其解析为object[],然后使用它来构建更好的复杂类型:

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

你应该对此进行更多的验证,但这是一个好的开始......