有哪些选项可以将.dl编码的表单数据转换为.Net中的JSON

时间:2022-10-24 15:12:25

I have a web request that is sending the server data that is in the format application/x-www-form-urlencoded. I would like to convert it to application/json.

我有一个Web请求,它发送的服务器数据格式为application / x-www-form-urlencoded。我想将它转换为application / json。

Example:

例:

URL-encoded form data:

URL编码的表单数据:

Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d

Pretty version:

漂亮版本:

Property1=A
Property2=B
Property3[0][SubProperty1]=a
Property3[0][SubProperty2]=b
Property3[1][SubProperty1]=c
Property3[1][SubProperty2]=d

The above data needs to be converted to the following JSON data:

以上数据需要转换为以下JSON数据:

{
    Property1: "A",
    Property2: "B",
    Property3: [
        { SubProperty1: "a", SubProperty2: "b" },
        { SubProperty1: "c", SubProperty2: "d" }]
}

Question:

题:

Are there any free tools that are capable of doing this? I have not been able to find any myself and if they exist, I'd rather consume them than writing one myself, but if it comes to that, I will.

有没有能够做到这一点的免费工具?我一直无法找到自己,如果它们存在,我宁愿消耗它们而不是自己写一个,但如果是这样,我会。

A C#/.Net solution is preferred.

C#/ .Net解决方案是首选。

2 个解决方案

#1


20  

I've written a utility class for parsing query strings and form data. It's available at:

我编写了一个实用程序类来解析查询字符串和表单数据。它可以在:

https://gist.github.com/peteroupc/5619864

https://gist.github.com/peteroupc/5619864

Example:

例:

// Example query string from the question
String test="Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d";
// Convert the query string to a JSON-friendly dictionary
var o=QueryStringHelper.QueryStringToDict(test);
// Convert the dictionary to a JSON string using the JSON.NET
// library <http://json.codeplex.com/>
var json=JsonConvert.SerializeObject(o);
// Output the JSON string to the console
Console.WriteLine(json);

Let me know if it works for you.

请让我知道这对你有没有用。

#2


8  

The .NET Framework 4.5 includes everything you need to convert url-encoded form data to JSON. In order to do this you have to add a reference to the namespace System.Web.Extension in your C# project. After that you can use the JavaScriptSerializer class which provides you with everything you need to do the conversion.

.NET Framework 4.5包含将URL编码的表单数据转换为JSON所需的一切。为此,您必须在C#项目中添加对命名空间System.Web.Extension的引用。之后,您可以使用JavaScriptSerializer类,它为您提供进行转换所需的一切。

The Code

代码

using System.Web;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var dict = HttpUtility.ParseQueryString("Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d");
            var json = new JavaScriptSerializer().Serialize(
                                                     dict.Keys.Cast<string>()
                                                         .ToDictionary(k => k, k => dict[k]));

            Console.WriteLine(json);
            Console.ReadLine();
        }
    }
}

The Output

输出

{
    "Property1":"A",
    "Property2":"B",
    "Property3[0][SubProperty1]":"a",
    "Property3[0][SubProperty2]":"b",
    "Property3[1][SubProperty1]":"c",
    "Property3[1][SubProperty2]":"d"
}

Notice: The output does not contain linebreaks or any formatting

注意:输出不包含换行符或任何格式

Source: How do I convert a querystring to a json string?

来源:如何将查询字符串转换为json字符串?

#1


20  

I've written a utility class for parsing query strings and form data. It's available at:

我编写了一个实用程序类来解析查询字符串和表单数据。它可以在:

https://gist.github.com/peteroupc/5619864

https://gist.github.com/peteroupc/5619864

Example:

例:

// Example query string from the question
String test="Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d";
// Convert the query string to a JSON-friendly dictionary
var o=QueryStringHelper.QueryStringToDict(test);
// Convert the dictionary to a JSON string using the JSON.NET
// library <http://json.codeplex.com/>
var json=JsonConvert.SerializeObject(o);
// Output the JSON string to the console
Console.WriteLine(json);

Let me know if it works for you.

请让我知道这对你有没有用。

#2


8  

The .NET Framework 4.5 includes everything you need to convert url-encoded form data to JSON. In order to do this you have to add a reference to the namespace System.Web.Extension in your C# project. After that you can use the JavaScriptSerializer class which provides you with everything you need to do the conversion.

.NET Framework 4.5包含将URL编码的表单数据转换为JSON所需的一切。为此,您必须在C#项目中添加对命名空间System.Web.Extension的引用。之后,您可以使用JavaScriptSerializer类,它为您提供进行转换所需的一切。

The Code

代码

using System.Web;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var dict = HttpUtility.ParseQueryString("Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d");
            var json = new JavaScriptSerializer().Serialize(
                                                     dict.Keys.Cast<string>()
                                                         .ToDictionary(k => k, k => dict[k]));

            Console.WriteLine(json);
            Console.ReadLine();
        }
    }
}

The Output

输出

{
    "Property1":"A",
    "Property2":"B",
    "Property3[0][SubProperty1]":"a",
    "Property3[0][SubProperty2]":"b",
    "Property3[1][SubProperty1]":"c",
    "Property3[1][SubProperty2]":"d"
}

Notice: The output does not contain linebreaks or any formatting

注意:输出不包含换行符或任何格式

Source: How do I convert a querystring to a json string?

来源:如何将查询字符串转换为json字符串?