加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 有哪些选项可以将网址编码的表单数据转换为.Net中的JSON

发布时间:2020-12-15 06:51:46 所属栏目:百科 来源:网络整理
导读:我有一个Web请求发送格式为application / x-www-form-urlencoded的服务器数据.我想把它转换为application / json. 例: 网址编码表单资料: Property1=AProperty2=BProperty3%5B0%5D%5BSubProperty1%5D=aProperty3%5B0%5D%5BSubProperty2%5D=bProperty3%5B1%
我有一个Web请求发送格式为application / x-www-form-urlencoded的服务器数据.我想把它转换为application / json.

例:

网址编码表单资料:

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

漂亮版本:

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

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

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

题:

有没有任何能够做到这一点的免费工具?我没有找到任何自己,如果他们存在,我宁愿消费他们而不是自己写一个,但如果是这样,我会的.

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

解决方法

我已经编写了一个用于解析查询字符串和表单数据的实用程序类.可在:

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

例:

// 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);

让我知道如果它适用于你

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读