更新时间:2022-12-27 17:57:52
您可以将SupportMultipleContent
标志设置为true的JsonTextReader
来读取此非标准JSON.假设您有一个如下所示的类Person
:
You can use a JsonTextReader
with the SupportMultipleContent
flag set to true to read this non-standard JSON. Assuming you have a class Person
that looks like this:
class Person
{
public string Name { get; set; }
}
您可以像这样反序列化JSON对象:
You can deserialize the JSON objects like this:
string json = @"{""name"": ""John""}{""name"": ""Joe""}";
using (StringReader sr = new StringReader(json))
using (JsonTextReader reader = new JsonTextReader(sr))
{
reader.SupportMultipleContent = true;
var serializer = new JsonSerializer();
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
Person p = serializer.Deserialize<Person>(reader);
Console.WriteLine(p.Name);
}
}
}