行分隔的json序列化和反序列化

我正在使用JSON.NET和C#5.我需要序列化/反序列化对象的列表成行分隔的JSON。 http://en.wikipedia.org/wiki/Line_Delimited_JSON 。 例,

{"some":"thing1"} {"some":"thing2"} {"some":"thing3"} 

 {"kind": "person", "fullName": "John Doe", "age": 22, "gender": "Male", "citiesLived": [{ "place": "Seattle", "numberOfYears": 5}, {"place": "Stockholm", "numberOfYears": 6}]} {"kind": "person", "fullName": "Jane Austen", "age": 24, "gender": "Female", "citiesLived": [{"place": "Los Angeles", "numberOfYears": 2}, {"place": "Tokyo", "numberOfYears": 2}]} 

为什么我需要它,因为它的Google BigQuery要求https://cloud.google.com/bigquery/preparing-data-for-bigquery

更新:我发现的一种方法是,序列化每个对象分开,并join新行结束。

您可以通过使用JsonTextReader手动parsing您的JSON并将SupportMultipleContent标志设置为true

如果我们看看你的第一个例子,并创build一个名为Foo的POCO:

 public class Foo { [JsonProperty("some")] public string Some { get; set; } } 

这是我们如何parsing它:

 var json = "{\"some\":\"thing1\"}\r\n{\"some\":\"thing2\"}\r\n{\"some\":\"thing3\"}"; var jsonReader = new JsonTextReader(new StringReader(json)) { SupportMultipleContent = true // This is important! }; var jsonSerializer = new JsonSerializer(); while (jsonReader.Read()) { Foo foo = jsonSerializer.Deserialize<Foo>(jsonReader); }