我有以下格式的字符串
"Name":"ABC","Address":"Street1,Street2","City":"Pune"
我希望输出为
Name = ABC
Address = Street1, Street2
City = Pune
我正在使用的代码是
public string[] GetData(string op)
{
MDB result = new MDB();
var split = op.Split(':');
string[] keyValue;
foreach (string s in split)
{
keyValue = s.Split(',');
try
{
PropertyInfo propertyInfo = result.GetType().GetProperty(keyValue[0]);
if (propertyInfo != null)
{
propertyInfo.SetValue(result, Convert.ChangeType(keyValue[1], propertyInfo.PropertyType), null);
}
}
catch (Exception ex)
{
}
}
}
然而,当我将文本拆分为","我得到以下结果
Name= ABC
Address = Street1
如何在地址中获取Street2?
答案 0 :(得分:0)
看起来像JSON,一些简单的反序列化可以完成任务
public class User
{
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
}
private static void jsonDeserialize()
{
var json = "{\"Name\":\"ABC\",\"Address\":\"Street1,Street2\",\"City\":\"Pune\"}";
User user = JsonConvert.DeserializeObject<User>(json);
Console.WriteLine("Name = "+ user.Name);
Console.WriteLine("Address = " + user.Address);
}
结果:
Name = ABC
Address = Street1,Street2