POST字典为JSON

编程入门 行业动态 更新时间:2024-10-12 01:26:26
本文介绍了POST字典为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将Dictionary中的某些字段(来自VB.Net应用程序)以JSON格式发送到Web服务.

I need to send some fields (from VB.Net application) in Dictionary as JSON format to web services.

下面是代码:

Dim dict As New Dictionary(Of String, String) dict.Add("Drinks", "2") dict.Add("ID", "1") Dim parameters As String = JsonConvert.SerializeObject(dict) Dim jsonString As String = "{""Pram"":""" + parameters + """}" Dim Uri As New Uri(String.Format("***/WebServices/UpdateSQL")) Dim data = Encoding.Default.GetBytes(jsonString) Dim result_post As String = SendRequest(Uri, data, "application/json", "POST")

在我的webservices(ASP.NET c#)中,我试图按下面的代码捕获上面的字符串

In my webservices(ASP.NET c#) I am trying to capture above string as in below code

public ActionResult UpdateSQL(string Pram)

我收到错误代码500:内部服务器错误.我尝试从VB应用程序传递单个字段,并且效果很好.下面是我发送单个字段的代码

I am getting error code 500: Internal server error. I tried passing individual fields from VB application and that works fine. Below is code if I send individual fields

Dim jsonString As String = "{""SQL"":""" + sSQL + """," & """TableName"":""" + tableName + """," & """Drinks"":""""2"",""ID"":""1""}"

上面的json字符串在发送时工作正常,并在webservices中被捕获,如下代码:

The above json string when sent works absolutely fine and are captured in webservices as below code:

public ActionResult UpdateSQL(string SQL, string TableName, string Drinks, string ID)

仅当我将Dictionary序列化为字符串并尝试发送时出现内部服务器错误. 请告知我是否有什么遗漏.谢谢

It's only when I serialize Dictionary as string and try to send,getting Internal Server Error. Please advise if I am missing something.Thanks

推荐答案

Dim dict As New Dictionary(Of String, String) dict.Add("Drinks", "2") dict.Add("ID", "1") Dim parameters As String = JsonConvert.SerializeObject(dict, Formatting.None) Dim Uri As New Uri(String.Format("localhost:60627/home/test/")) Dim webClient As New WebClient() Dim resByte As Byte() Dim resString As String Dim reqString() As Byte webClient.Headers("content-type") = "application/json" Dim senddata As Object = JsonConvert.SerializeObject(New With {Key .param = parameters}).ToString() reqString = Encoding.Default.GetBytes(senddata) resByte = webClient.UploadData(Uri, "post", reqString) resString = Encoding.Default.GetString(resByte)

首先更改这种晕染"类型 "{"婴儿车":"" +参数+"} " 因为这让我感到困惑.

First change this type of Concatination "{""Pram"":""" + parameters + """}" for It's Make Little confustion.

并且由于问题是逃逸序列(未向Concat正确分配数据),数据无法发送到服务器属性.

And The Data Not Sending To the Server Property Because problem is Escapse Sequence That Concat is Not Given Proper Serialization of the Data.

在上面,我将代码更改为

In Above I Change the Code For

Dim parameters As String = JsonConvert.SerializeObject(dict, Formatting.None)

更改数据

{饮料":"2","ID":"1"}

{"Drinks":"2","ID":"1"}

下一个序列化将改变

{"param":"{\" Drinks \:\" 2 \,\" ID \:\" 1 \}"}

{"param":"{\"Drinks\":\"2\",\"ID\":\"1\"}"}

但是您的发送数据序列化是

But Your Serlization of Send Data is

{"param":"{" Drinks:" 2," ID:" 1}"}

{"param":"{"Drinks":"2","ID":"1"}"}

因此,数据发送不正确

So, the Data is Not Sending Properly

我被检查妥当了....

I was Checked Properly it's Working Fine....

更多推荐

POST字典为JSON

本文发布于:2023-11-16 03:57:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   POST   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!