如何转换\在vb.net HttpRequest的使用newtonsoft.json.linq.serializeobject?

问题描述 投票:0回答:1

我一直在使用Newtonsoft JObject创建一个JSON对象,但我得到一个错误的请求错误,当我尝试提交它,如果任何属性有空格,斜线等。

updatestring = "date=2/14/2019"
Dim jobjattr As New Newtonsoft.Json.Linq.JObject(
    New Newtonsoft.Json.Linq.JProperty("description", "test"),
    New Newtonsoft.Json.Linq.JProperty("source", updatestring)
)
Dim jobjdat As New Newtonsoft.Json.Linq.JObject(
    New Newtonsoft.Json.Linq.JProperty("type", "synch_log"),
    New Newtonsoft.Json.Linq.JProperty("id", "6278042e-ed64-0418-a651-5c574dc4f12b"),
    New Newtonsoft.Json.Linq.JProperty("attributes", jobjattr)
)
Dim jobj As New Newtonsoft.Json.Linq.JObject(New Newtonsoft.Json.Linq.JProperty("data", jobjdat))

Dim jsonserializersettings As New Newtonsoft.Json.JsonSerializerSettings
jsonserializersettings.StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeNonAscii
Dim stringReq = Newtonsoft.Json.JsonConvert.SerializeObject(jobj, jsonserializersettings)

Dim byteData As Byte() = System.Text.Encoding.UTF8.GetBytes(stringReq)
httprequest.ContentLength = byteData.Length
Dim postreqstream As System.IO.Stream = .GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()

传入jobj = { “数据”:{ “类型”: “synch_log”, “ID”: “6278042e-ed64-0418-a651-5c574dc4f12b”, “属性”:{ “描述”: “测试”, “源”: “日期= 2/14/2019” 表}}}

后serialzation byteData仍然= { “数据”:{ “类型”: “synch_log”, “ID”: “6278042e-ed64-0418-a651-5c574dc4f12b”, “属性”:{ “描述”: “测试”,“源“:” 日期= 2/14 / 2019" 表}}}

我希望将逃过/。任何文本字符串工作正常,我自己也尝试jsonserializer设置为默认和EscapeHtml但具有相同的结果。

其它字符会导致相同eror。 “datetoday”的帖子正确,但“日期=今天”和“今天几号”导致400错误的请求错误

最接近的答案,我发现是,也许对象被双逃脱,但我不能看到这将是。

vb.net http json.net suitecrm
1个回答
0
投票

谢谢大家。布莱恩,你使我在正确的方向。我忘了,这是一个API调用来SuiteCRM但你的问题让我思考一下在服务器端,它原来有一个与V8 API一个悬而未决的bug。我只是认为这是我的代码。

github bug report

© www.soinside.com 2019 - 2024. All rights reserved.