c#API“解析值时遇到意外字符:S.路径",第0行,位置0"; [英] c# API "Unexpected character encountered while parsing value: S. Path '', line 0, position 0"
问题描述
我使用c#编码的多个API,效果很好.我想使用一个接收匿名对象的对象(我不想创建一个类).尝试反序列化对象时遇到问题.
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.
我有一个遵循此方案的API,当使用json_dumps
函数从python
调用它时,它可以很好地工作.但是,当我尝试使用JSON.stringify
(来自a甚至是POSTMAN时,我有一个400错误的请求.
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>
等待一个字符串,我尝试过:
JsonConvert.DeserializeObject<dynamic>
waits for a string, I tried:
-在myMethod
的参数中将objectSentByUser
指定为字符串
这样做的时候,我什至没有输入方法就得到了400(我尝试发送JSON,添加引号,发送字符串等...)
-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...)
-使用(string)objectSentByUser
进行投射,不起作用
-to cast with (string)objectSentByUser
, it doesn't work
-使用toString()
方法,这将导致下一个错误:
解析值时遇到意外字符:S.路径",第0行,位置0
这是很正常的,因为objectSentByUser.toString()
返回"System.Object"
(但是为什么在与python json_dump一起使用时能起作用?)
-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?)
此代码在通过python函数json_dump调用时有效,该函数返回这样的对象:
This code works when called with python function json_dump that returns an object like this:
"{\\" key1 \\:\\" value1 \\,...}"
"{\\"key1\\":\\"value1\\",...}"
我从邮递员发送了一个经典的POST,其中application/json
为contentType
,并且正文中包含有效的JSON
(已在另一个关于stackoverflow的讨论中找到的网站上进行了验证)
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)
非常感谢您的帮助
再见
推荐答案
如果用户向您的操作发送了有效的json字符串,则不接受对象作为参数,而是接受字符串(即,因为您的用户发送了你一个).
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).
如果在对象上调用ToString()
,则可能不是Json
格式.
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);
这篇关于c#API“解析值时遇到意外字符:S.路径",第0行,位置0";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!