2 回答
TA贡献1784条经验 获得超7个赞
您应该为此使用一些 JSON 库(例如Json.net)。这将解决在手动操作 JSON 字符串时可能会遗漏的许多陷阱。
var o1 = JsonConvert.Deserialize<JObject>(jsonstring);
//you can split by ' ' and ',' because email addresses won't contain any whitespaces. For other purposes you may need better splitting rules.
var to = o1.Value<string>("To").Split(new char[]{' ', ',"}, StringSplitOptions.RemoveEmptyEntries);
var cc = o1.Value<string>("CC").Split(new char[]{' ', ',"}, StringSplitOptions.RemoveEmptyEntries);
var bcc = o1.Value<string>("BCC").Split(new char[]{' ', ',"}, StringSplitOptions.RemoveEmptyEntries);
var outstring = JsonConvert.SerializeObject(new JObject{
{"To", JArray.FromObject(to)},
{"CC", JArray.FromObject(cc)},
{"BCC", JArray.FromObject(bcc)},
});
请注意,没有错误处理。例如,如果原始字符串错过了三个列表之一,这将抛出。
TA贡献1828条经验 获得超3个赞
使用 Newtonsoft.Json
string strJson = @"{'To':'name @company.com, name2 @company.com, name3 @company.com','CC':'namecc @company.com','BCC':''}";
dynamic jsonObject = JsonConvert.DeserializeObject(strJson);
Dictionary<string, string[]> val = new Dictionary<string, string[]>();
foreach (var prop in jsonObject)
{
string name = prop.Name;
string[] datas = Convert.ToString(prop.Value).Split(',');
val.Add(name, datas);
}
string op = JsonConvert.SerializeObject(val);
- 2 回答
- 0 关注
- 194 浏览
添加回答
举报