[英]Passing JSON as a string in the body of the POST request
I need a small help, because I don't know how to solve the below problem.我需要一点帮助,因为我不知道如何解决以下问题。
The requirement is simple, I have to sent the JSON to the server as a string parameter.要求很简单,我必须将 JSON 作为字符串参数发送到服务器。 The server basing on the key finds the mapping, and generically parses the JSON to some objects.
服务器根据key找到映射,一般将JSON解析为一些对象。 That means, that the payload can have a different values and structures, each key has its own mapping - different data structure, number of parameters and so on.
这意味着,有效负载可以有不同的值和结构,每个键都有自己的映射 - 不同的数据结构、参数数量等。 So the payload shouldn't be parsed outside the endpoint logic.
因此,不应在端点逻辑之外解析有效负载。
I know, that the Swagger sees the payload as a JSON, not as a string, and it tries to parse the data.我知道,Swagger 将有效负载视为 JSON,而不是字符串,它会尝试解析数据。 How can I send the JSON as a string parameter to the endpooint without parsing the parameter?
如何在不解析参数的情况下将 JSON 作为字符串参数发送到端点? I have to parse it inside of the application, because of the mentioned mappings.
由于提到的映射,我必须在应用程序内部解析它。
Example JSON:示例 JSON:
{
"key": "test",
"payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}
When I'm trying to send the data in Swagger, I'm getting the below results:当我尝试在 Swagger 中发送数据时,我得到以下结果:
curl -X POST "http://localhost:5110/api/InboundData" -H "accept: */*" -H "Content-Type: application/json-patch+json" -d "{ \"key\": \"test\", \"payload\": \"[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]\"}"
{
"errors": {
"payload": [
"After parsing a value an unexpected character was encountered: I. Path 'payload', line 3, position 17."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|d952c89f-4e25126d8cdf3697."
}
Data model:数据 model:
[Required]
[JsonProperty(Required = Required.DisallowNull)]
[MaxLength(100)]
public string Key { get; set; }
[Required]
[JsonProperty(Required = Required.DisallowNull)]
public string Payload { get; set; }
The error clearly suggests that your JSON
is not correct.该错误清楚地表明您的
JSON
不正确。 If we analyze the payload property:如果我们分析有效载荷属性:
{
"key": "test",
"payload": "[{"IDNew":1,"NameNew":"t1","DescriptionNew":"t1d", "IntegerValueNew":1, "DecimalValueNew":123.3}]"
}
It seems you are creating a string object which further contains a JSON as a string.您似乎正在创建一个字符串 object ,其中还包含一个 JSON 作为字符串。 Generally, when you pass an array, you would pass it like this.
一般来说,当你传递一个数组时,你会像这样传递它。
{
"key": "test",
"payload": [
{
"IDNew": 1,
"NameNew": "t1",
"DescriptionNew": "t1d",
"IntegerValueNew": 1,
"DecimalValueNew": 123.3
}
]
}
But, since value of the payload
property is not properly escaped, which is why it is not properly able to parse it as it has unexpected characters for a string value.但是,由于
payload
属性的值没有被正确转义,这就是为什么它不能正确解析它,因为它有一个字符串值的意外字符。
If you strictly want to pass a JSON Array as a string object, you need to properly escape it in order to get it working:如果您严格希望将 JSON 数组作为字符串 object 传递,则需要正确转义它才能使其正常工作:
{
"key": "test",
"payload": "[{\"IDNew\":1,\"NameNew\":\"t1\",\"DescriptionNew\":\"t1d\", \"IntegerValueNew\":1, \"DecimalValueNew\":123.3}]"
}
This is how you would escape your JSON
if you strictly want to pass a JSON object that further contains JSON as string.如果您严格希望通过 JSON
JSON
进一步包含 Z0ECD11C1D7A2D874A22.
Or, perhaps, use single quote (') instead for the nested JSON
:或者,也许,使用单引号 (') 代替嵌套的
JSON
:
{
"key": "test",
"payload": "[{'IDNew':1,'NameNew':'t1','DescriptionNew':'t1d', 'IntegerValueNew':1, 'DecimalValueNew':123.3}]"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.