如何从json流中反序列化多个对象?

好的,我之前已经问了类似这样的问题,但这是一个不同的主题,所以我觉得我应该做一个关于它的新话题。 对不起,如果这是我不应该做的事情……

无论如何:

我正在阅读twitterfeed并试图将其转换为丢失(状态)对象。 我现在的代码如下,但失败了:

webRequest = (HttpWebRequest)WebRequest.Create(stream_url); webRequest.Credentials = new NetworkCredential(username, password); webRequest.Timeout = -1; webResponse = (HttpWebResponse)webRequest.GetResponse(); Encoding encode = Encoding.GetEncoding("utf-8"); responseStream = new StreamReader(webResponse.GetResponseStream(), encode); int i = 0; //Read the stream. while (_running) { jsonText = responseStream.ReadLine(); byte[] sd = Encoding.Default.GetBytes(jsonText); stream.Write(sd, i, i + sd.Length); try { status s = json.ReadObject(stream) as status; if (s != null) { //write s to a file/collection or w/e i = 0; } } catch { } } 

想法是:将流复制到另一个流中。 并继续尝试读取它,直到发现状态对象。 这是为了防止流变小,所以它有机会成长。 当然,流并不总是从对象的开始处开始,或者可能是腐败的。

现在我确实找到了方法IsStartObject ,我想我应该使用它。 虽然我没有使用流的经验,但我永远找不到如何使用它的好例子。

是否有人可以向我解释如何从流中读取多个对象,以便我可以将它们写入列表或w / e。 我真的找不到互联网上的任何好例子..

非常感谢您的尝试!!!

我使用了Json.Net库和这个扩展类 ,它使用DynamicObject来解析流式json对象

 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://stream.twitter.com/1/statuses/sample.json"); webRequest.Credentials = new NetworkCredential("...", "......"); webRequest.Timeout = -1; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); Encoding encode = Encoding.GetEncoding("utf-8"); StreamReader responseStream = new StreamReader(webResponse.GetResponseStream()); string line; while (true) { line = responseStream.ReadLine(); dynamic obj = JsonUtils.JsonObject.GetDynamicJsonObject(line); if(obj.user!=null) Console.WriteLine(obj.user.screen_name + " => " + obj.text); } 

这是LB通过计算{和}的嵌套级别进行拆分的建议的实现。

 public static IEnumerable JsonSplit(this StreamReader input, char openChar = '{', char closeChar = '}', char quote='"', char escape='\\') { var accumulator = new StringBuilder(); int count = 0; bool gotRecord = false; bool inString = false; while (!input.EndOfStream) { char c = (char)input.Read(); if (c == escape) { accumulator.Append(c); c = (char)input.Read(); } else if (c == quote) { inString = !inString; } else if (inString) { } else if (c == openChar) { gotRecord = true; count++; } else if (c == closeChar) { count--; } accumulator.Append(c); if (count != 0 || !gotRecord) continue; // now we are not within a block so string result = accumulator.ToString(); accumulator.Clear(); gotRecord = false; yield return result; } } 

这是一个测试

 [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { string text = "{\"a\":1}{\"b\":\"hello\"}{\"c\":\"oh}no!\"}{\"d\":\"and\\\"also!\"}"; var reader = FromStackOverflow.GenerateStreamFromString(text); var e = MyJsonExtensions.JsonSplit(reader).GetEnumerator(); e.MoveNext(); Assert.AreEqual("{\"a\":1}", e.Current); e.MoveNext(); Assert.AreEqual("{\"b\":\"hello\"}", e.Current); e.MoveNext(); Assert.AreEqual("{\"c\":\"oh}no!\"}", e.Current); e.MoveNext(); Assert.AreEqual("{\"d\":\"and\\\"also!\"}", e.Current); } } 

GenerateStreamFromString的实现就在这里