如何从 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.我真的在互联网上找不到任何好的例子..
非常感谢您的尝试!
Ok, I have asked questions about something like this before, but this is a different subject, so i feel i should make a new topic about it. I'm sorry if this is something i should not have done...
Anyway:
I'm currently reading a twitterfeed and trying to convert it to lose (status) objects. The code i have now is as follows but fails:
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
{
}
}
The idea is: Copy the stream into another stream. and keep trying to read it untill an status object is discovered.
This was ment to prevent the stream for being to little, so it had chance to grow. Ofcourse the stream does not always start at the start of an object, or can be corrupt.
Now i did find the method IsStartObject
, and i think i should use it.
Though i have no experience with streams and i can never find a good example of how to use this.
Is there anyone who can explain to me how to read multiple objects from the stream so i can write them into a list or w/e. I really can't find any good examples on the internets..
Thank you very much for trying!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用了 Json.Net 库和 这个扩展类,利用
DynamicObject
来解析流式json对象I used Json.Net library and this extension class that makes use of
DynamicObject
to parse streaming json objects这是 LB 通过计算 { 和 } 的嵌套级别进行拆分的建议的实现。
这是一个测试
GenerateStreamFromString 的实现在这里
This is an implementation of L.B's suggestion for splitting by counting the nesting level of { and }.
Here's a test
The implementation of GenerateStreamFromString is here