哈希表输出 Asp.net MVC

发布于 2024-12-22 18:22:53 字数 1342 浏览 1 评论 0原文

有人可以在response.write部分帮助我吗?会是什么?

哎呀!您的问题无法提交,因为: 您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的情况。

   public void Test()
    {

        Hashtable hs = new Hashtable();
        ArrayList a = new ArrayList();

        WebRequest wr = WebRequest.Create(string.Format("http://search.twitter.com/search.json?q={0}&rpp={1}&page={2}", "seni seviyorum", 20, 1));
        Stream s = wr.GetResponse().GetResponseStream();
        StreamReader sr = new StreamReader(s);
        string Sonuc = sr.ReadToEnd();

        hs = (Hashtable)JSON.JsonDecode(Sonuc);

        string from_user = hs["from_user"] != null ? hs["from_user"].ToString() : "";
        string to_user = hs["to_user"] != null ? hs["to_user"].ToString() : "";
        string text = hs["text"] != null ? hs["text"].ToString() : "";
        string profile_image_url = hs["profile_image_url"] != null ? hs["profile_image_url"].ToString() : "";
        string tweet_id = hs["id"] != null ? hs["id"].ToString() : "";
        DateTime created_at = hs["created_at"] != null ? DateTime.Parse(hs["created_at"].ToString()) : DateTime.Now;
        string twitter_url = string.Format("http://twitter.com/{0}/statuses/{1}", from_user, tweet_id);

        foreach (Hashtable oItem in (ArrayList)hs["results"])
        {
            Response.Write(oItem.?);
        }
    }

Anybody can help me on response.write section? What it would be?

Oops! Your question couldn't be submitted because:
Your post does not have much context to explain the code sections; please explain your scenario more clearly.

   public void Test()
    {

        Hashtable hs = new Hashtable();
        ArrayList a = new ArrayList();

        WebRequest wr = WebRequest.Create(string.Format("http://search.twitter.com/search.json?q={0}&rpp={1}&page={2}", "seni seviyorum", 20, 1));
        Stream s = wr.GetResponse().GetResponseStream();
        StreamReader sr = new StreamReader(s);
        string Sonuc = sr.ReadToEnd();

        hs = (Hashtable)JSON.JsonDecode(Sonuc);

        string from_user = hs["from_user"] != null ? hs["from_user"].ToString() : "";
        string to_user = hs["to_user"] != null ? hs["to_user"].ToString() : "";
        string text = hs["text"] != null ? hs["text"].ToString() : "";
        string profile_image_url = hs["profile_image_url"] != null ? hs["profile_image_url"].ToString() : "";
        string tweet_id = hs["id"] != null ? hs["id"].ToString() : "";
        DateTime created_at = hs["created_at"] != null ? DateTime.Parse(hs["created_at"].ToString()) : DateTime.Now;
        string twitter_url = string.Format("http://twitter.com/{0}/statuses/{1}", from_user, tweet_id);

        foreach (Hashtable oItem in (ArrayList)hs["results"])
        {
            Response.Write(oItem.?);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-12-29 18:22:53

您最好创建一个比哈希表更好的数据结构来存储结果中的信息。我确信这样的例子有很多。

我不确定你有什么 json 库,但我很幸运地使用了 newtonsoft.Json - 可通过 nuget 安装。

所以无论如何,这里的主要问题是哈希表并不适合从中获取数据。事实上,您可以研究返回的 json,然后找出下一个元素级别是什么。

嗅探 json,你可以看到 hte return 的结构看起来像这样(猜测我想这也可能存储为内部哈希表,尽管我无法调试它来告诉):

{ "created_at" : "Fri, 23 Dec 2011 13:56:34 +0000",
    "from_user" : "Canboonn",
    "from_user_id" : 251744686,
    "from_user_id_str" : "251744686",
    "from_user_name" : "Bayram Can Avci",
    "geo" : null,
    "id" : 150213210446897152,
    "id_str" : "150213210446897152",
    "iso_language_code" : "tr",
    "metadata" : { "result_type" : "recent" },
    "profile_image_url" : "http://a1.twimg.com/profile_images/1700040754/IMG_8783_normal.JPG",
    "profile_image_url_https" : "https://si0.twimg.com/profile_images/1700040754/IMG_8783_normal.JPG",
    "source" : "<a href="http://twitter.com/">web</a>",
    "text" : "30 Aralik'a kadar 10 TL yüklersem her yöne 100 dakika, 100 sms ve 100 MB internet hediyeee, seviyorum seni #vodafone",
    "to_user" : null,
    "to_user_id" : null,
    "to_user_id_str" : null,
    "to_user_name" : null
  }

所以你可以去

foreach (Hashtable oItem in (ArrayList)hs["results"])
{
   Response.Write(oItem["from_user"]);
}

如果这不起作用,然后尝试查看 oItem.Keys 属性并查看其中的内容。也许还有一些线索。

例如写入结果的属性之一。

You are best here to create a data structure that is better than a hashtable at storing the information in the results. I am sure there are plenty of examples of this.

I'm not sure what json library you have but I have had a lot of luck with newtonsoft.Json - installable by nuget.

So anyway the major issue here is that Hashtables aren't really great for getting data out of. As it is you can investigate the json returned and then figure out what the next level of element is.

Sniffing at the json you can see that the structure of hte return looks like this (at a guess I'd imagine this might be stored as an inner hashtable as well though I can't debug it to tell) :

{ "created_at" : "Fri, 23 Dec 2011 13:56:34 +0000",
    "from_user" : "Canboonn",
    "from_user_id" : 251744686,
    "from_user_id_str" : "251744686",
    "from_user_name" : "Bayram Can Avci",
    "geo" : null,
    "id" : 150213210446897152,
    "id_str" : "150213210446897152",
    "iso_language_code" : "tr",
    "metadata" : { "result_type" : "recent" },
    "profile_image_url" : "http://a1.twimg.com/profile_images/1700040754/IMG_8783_normal.JPG",
    "profile_image_url_https" : "https://si0.twimg.com/profile_images/1700040754/IMG_8783_normal.JPG",
    "source" : "<a href="http://twitter.com/">web</a>",
    "text" : "30 Aralik'a kadar 10 TL yüklersem her yöne 100 dakika, 100 sms ve 100 MB internet hediyeee, seviyorum seni #vodafone",
    "to_user" : null,
    "to_user_id" : null,
    "to_user_id_str" : null,
    "to_user_name" : null
  }

So you can go

foreach (Hashtable oItem in (ArrayList)hs["results"])
{
   Response.Write(oItem["from_user"]);
}

If this doesnt work then try look at the oItem.Keys property and see waht is in there. There might be a few more clues.

For example to write one of the properties of the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文