在 C# 中将查询字符串序列化为 Json - 值不会显示,仅显示键。为什么?
我正在尝试在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,查询字符串是NameValueCollection,如何序列化NameValueCollection在这里:如何将NameValueCollection转换为JSON 字符串?
Well, Query string is NameValueCollection, and how to serialize NameValueCollection is here: how to convert NameValueCollection to JSON string?
它的计算结果是一个
Dictionary
,它很容易被 JavaScriptSerializer 或 Newtonsoft 的 Json.Net 序列化:Request.QueryString
中的任何重复键最终都会作为单个键字典,其值连接在一起,并用逗号分隔。当然,这也适用于任何
NameValueCollection
,而不仅仅是Request.QueryString
。This evaluates to a
Dictionary<string,string>
which is readily serializable by JavaScriptSerializer or Newtonsoft's Json.Net: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 justRequest.QueryString
.