什么是一些选项来转换地址恩codeD表单数据到JSON在.net [英] What Are Some Options To Convert Url-Encoded Form Data to JSON in .Net

查看:233
本文介绍了什么是一些选项来转换地址恩codeD表单数据到JSON在.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是向服务器发送数据的格式为应用程序/ x-WWW的形式urlen codeD Web请求。我想将其转换为应用程序/ JSON

示例:

URL-CN codeD格式的数据:

<$p$p><$c$c>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的版本:

  Property1 = A
Property2 = B
Property3 [0] [SubProperty1] =一
Property3 [0] [SubProperty2] = b的
Property3 [1] [SubProperty1] = C
Property3 [1] [SubProperty2] = D
 

需要上面的数据要转换成以下JSON数据:

  {
    Property1:A,
    Property2:B
    Property3:
        {SubProperty1:一个,SubProperty2:B},
        {SubProperty1:C,SubProperty2:D}]
}
 

问:

有没有免费的工具,这些工具能够做到这一点?我一直没能找到任何自己,如果存在的话,我宁愿消耗他们不是写一个自己的,但如果涉及到的是,我会的。

一个C#/。NET解决方案是preferred。

解决方案

我已经写了解析查询字符串和表单数据的工具类。它可在:

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

例如:

从这个问题

  //例查询字符串
串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";
//将查询字符串的JSON友好字典
变种O = QueryStringHelper.QueryStringToDict(试验);
//使用JSON.NET字典转换为JSON字符串
//库&LT; HTTP://json.$c$cplex.com/>
VAR JSON = JsonConvert.SerializeObject(O);
//输出JSON字符串到控制台
Console.WriteLine(JSON);
 

让我知道,如果你的作品。

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.

Example:

URL-encoded form data:

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:

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

解决方案

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

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.

这篇关于什么是一些选项来转换地址恩codeD表单数据到JSON在.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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