问题
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