问题描述
我使用了多个用 c# 编写的 API,效果很好.我想用一个接收匿名对象(我不想创建一个类).当我尝试反序列化对象时遇到问题.
我有一个遵循此方案的 API,它在使用 json_dumps 函数从 python 调用时运行良好.但是当我尝试使用 JSON.stringify 时(来自 a 甚至 POSTMAN,我有一个 400 错误请求.
这是我的代码,我尝试了很多东西:
[WebInvoke(Method = "POST", UriTemplate = "myUrl")] [OperationContract] public Message myMethod(object objectSentByUser) { var perso = JsonConvert.DeserializeObject<dynamic>(objectSentByUser.ToString());
JsonConvert.DeserializeObject<dynamic> 等待一个字符串,我试过了:
- 将 objectSentByUser 指定为 myMethod 的参数中的字符串当我这样做时,我什至没有输入方法就得到了 400(我尝试发送 JSON、添加引号、发送字符串等......)
-用(string)objectSentByUser进行投射,它不起作用
- 使用 toString() 方法,这会导致下一个错误:解析值时遇到意外字符:S. Path '', line 0, position 0这很正常,因为 objectSentByUser.toString() 返回"System.Object"(但为什么它在与 python json_dump 一起使用时会起作用?)
此代码在使用 python 函数 json_dump 调用时有效,该函数返回如下对象:
"{\\"key1\\":\\"value1\\",...}"
我从 Postman 发送了一个经典的 POST,其中 application/json 作为 contentType 并且正文中有一个有效的 JSON(在另一个关于 stackoverflow 的讨论中找到的网站上验证)
非常感谢您的帮助
再见
推荐答案
如果用户向您的操作发送有效的 json 字符串,则不要接受对象作为参数,而是接受字符串(即因为您的用户发送你一个).
如果你在一个对象上调用 ToString(),很可能它不是 Json 格式.
尝试接受一个字符串并反序列化它:
[WebInvoke(Method = "POST", UriTemplate = "myUrl")] [OperationContract] public Message myMethod(string jsonSentByUser) { var perso = JsonConvert.DeserializeObject<dynamic>(jsonSentByUser);
问题描述
I use multiple API coded in c# that works well. I want to use one receiving an anonymous object (I don't want to create a Class). I have a problem when I try to deserialize the object.
I have an API following this scheme, it works well when it's called from python using the json_dumps function. But when I try with JSON.stringify (from an a or even POSTMAN, I have a 400 bad request.
Here is my code, I have tried a lot of things :
[WebInvoke(Method = "POST", UriTemplate = "myUrl")] [OperationContract] public Message myMethod(object objectSentByUser) { var perso = JsonConvert.DeserializeObject<dynamic>(objectSentByUser.ToString());
JsonConvert.DeserializeObject<dynamic> waits for a string, I tried:
-to specify objectSentByUser as a string in the argument of myMethod When I do so, I've got a 400 without even entering the method (I tried to send a JSON, to add quotes, to send a string etc...)
-to cast with (string)objectSentByUser, it doesn't work
-to use the toString() method, which leads to the next error: Unexpected character encountered while parsing value: S. Path '', line 0, position 0 which is quite normal because objectSentByUser.toString() returns "System.Object" (but why does it work when used with python json_dump?)
This code works when called with python function json_dump that returns an object like this:
"{\\"key1\\":\\"value1\\",...}"
From Postman I send a classic POST with application/json as contentType and a valid JSON in the body (verified on a website found in an another discussion on stackoverflow)
Thanks a lot for your help
See you
推荐答案
If the user sends a valid json string to your action, then don't accept an object as a parameter, but rather a string (i.e. because your user sends you one).
If you call ToString() on an object, chances are, it's not in a Json format.
Try accepting a string and deserializing that:
[WebInvoke(Method = "POST", UriTemplate = "myUrl")] [OperationContract] public Message myMethod(string jsonSentByUser) { var perso = JsonConvert.DeserializeObject<dynamic>(jsonSentByUser);