如何反序列化Json多个结果?

时间:2013-02-16 14:33:33

标签: c#

我有如下所示的字符串(2行在一个字符串变量中),如何在C#中使用JsonConvert类分成2个字符串并反序列化

 {"operation":"waiting","wait":12121212}
 {"operation":"result","firstname":"bill", "lastname":"last"}

1 个答案:

答案 0 :(得分:2)

您可以使用新行分隔符将字符串拆分为字符串数组,然后JSON反序列化每一行。要拆分字符串,您可以使用Split方法。

例如:

string input = "... your input string ...";
string[] lines = input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
    // you could use a JSON serializer here to deserialize the line
    // and possibly add it to some result collection
}