如何反序列化从一个JSON数据流的多个对象? [英] How to deserialize multiple objects from a json stream?

查看:236
本文介绍了如何反序列化从一个JSON数据流的多个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我问了一下这样的事情之前的问题,但是这是一个不同的主题,所以我觉得我应该做一个关于它的新课题。我很抱歉,如果这事我不应该那样做......

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...

总之:

目前,我正在读通过twitterfeed,并试图将其转换为失去(状态)的对象。在code,我现在是如下,但失败:

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
    {

    }

}

我们的想法是:流复制到另一个流。并不断尝试读取它,直到一个状态对象被发现。 这是彪prevent的流其事小,所以它有机会成长。 Ofcourse流不总是开始于一个对象的开始,或者可以是已损坏。

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.

现在,我没有找到方法 IsStartObject ,我想我应该使用它。 虽然我有流没有经验,我永远无法找到如何使用一个很好的例子。

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.

是否有任何人谁可以给我解释一下如何读取多个对象从流这样我就可以把它们写入一个列表或W / E。我真的找不到对网际网路任何很好的例子。

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..

非常感谢你的努力!

推荐答案

我用 Json.Net 图书馆和这个扩展类,它使用 DynamicObject 到分析流JSON对象

I used Json.Net library and this extension class that makes use of DynamicObject to parse streaming json objects

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);
}

这篇关于如何反序列化从一个JSON数据流的多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆