在 C# 中将查询字符串序列化为 Json - 值不会显示,仅显示键。为什么?

发布于 2024-12-05 08:21:28 字数 1624 浏览 10 评论 0原文

我正在尝试在 C# 中将查询字符串序列化为 JSON。我没有得到我预期的结果,希望有人能解释一下。出于某种原因,我只得到查询“名称”,而不是“值”。

   //Sample Query:
   http://www.mydomain.com/Handler.ashx?method=preview&appid=1234      


    //Generic handler code:
    public void ProcessRequest(HttpContext context)
    {
        string json = JsonConvert.SerializeObject(context.Request.QueryString);
        context.Response.ContentType = "text/plain";
        context.Response.Write(json);
    }

    //Returns something like this:
    ["method", "appid"]

    //I would expect to get something like this:
    ["method":"preview", "appid":"1234"]

有人知道如何获得类似于后一个示例输出的字符串吗?我也尝试过

string json = new JavaScriptSerializer().Serialize(context.Request.QueryString);

并获得与 Newtonsoft Json 相同的结果。

编辑-这是基于以下答案的最终工作代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Collections.Specialized;

namespace MotoAPI3
{

public class Json : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var dict = new Dictionary<string, string>();
        foreach (string key in context.Request.QueryString.Keys)
        {
            dict.Add(key, context.Request.QueryString[key]);
        }

        string json = new JavaScriptSerializer().Serialize(dict);
        context.Response.ContentType = "text/plain";
        context.Response.Write(json);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

I'm trying to serialize a querystring to JSON in C#. I'm not getting the results I expected, and am hoping someone can explain. Some reason I'm only getting the query "name" and not the "value".

   //Sample Query:
   http://www.mydomain.com/Handler.ashx?method=preview&appid=1234      


    //Generic handler code:
    public void ProcessRequest(HttpContext context)
    {
        string json = JsonConvert.SerializeObject(context.Request.QueryString);
        context.Response.ContentType = "text/plain";
        context.Response.Write(json);
    }

    //Returns something like this:
    ["method", "appid"]

    //I would expect to get something like this:
    ["method":"preview", "appid":"1234"]

Anyone know how to get a string resembling the latter sample output? I've also tried

string json = new JavaScriptSerializer().Serialize(context.Request.QueryString);

and gotten identical results as the Newtonsoft Json.

EDIT- Here's the final working code based on the answer below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Collections.Specialized;

namespace MotoAPI3
{

public class Json : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var dict = new Dictionary<string, string>();
        foreach (string key in context.Request.QueryString.Keys)
        {
            dict.Add(key, context.Request.QueryString[key]);
        }

        string json = new JavaScriptSerializer().Serialize(dict);
        context.Response.ContentType = "text/plain";
        context.Response.Write(json);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

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

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

发布评论

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

评论(2

乖乖哒 2024-12-12 08:21:28

嗯,查询字符串是NameValueCollection,如何序列化NameValueCollection在这里:如何将NameValueCollection转换为JSON 字符串?

Well, Query string is NameValueCollection, and how to serialize NameValueCollection is here: how to convert NameValueCollection to JSON string?

甜尕妞 2024-12-12 08:21:28

它的计算结果是一个 Dictionary ,它很容易被 JavaScriptSerializer 或 Newtonsoft 的 Json.Net 序列化:

Request.QueryString.AllKeys.ToDictionary(k => k, k => Request.QueryString[k])

Request.QueryString 中的任何重复键最终都会作为单个键字典,其值连接在一起,并用逗号分隔。

当然,这也适用于任何 NameValueCollection,而不仅仅是 Request.QueryString

This evaluates to a Dictionary<string,string> which is readily serializable by JavaScriptSerializer or Newtonsoft's Json.Net:

Request.QueryString.AllKeys.ToDictionary(k => k, k => Request.QueryString[k])

Any repeated keys in Request.QueryString end up as a single key in the dictionary, whose values are concatenated together separated by commas.

Of course this also works for any NameValueCollection, not just Request.QueryString.

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