POST字典为JSON [英] POST Dictionary as JSON

查看:113
本文介绍了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("http://***/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("http://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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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