简体   繁体   English

如何删除JSON字符串中的开头和结尾的双引号和所有反斜杠-C#

[英]How to remove opening and closing double quotes and all backslashes in a JSON string - C#

For performing a rest API call with C# I have to serialize a dictionary with parameters. 为了使用C#执行rest API调用,我必须使用参数序列化字典。 These parameters have to be placed inside the body of the request as JSON. 这些参数必须以JSON的形式放置在请求的主体内。

The API call is done with the help of RestSharp. API调用是在RestSharp的帮助下完成的。 And the send string is checked with fiddler. 然后用提琴手检查发送字符串。

request.AddBody(_jsonrequeststring);

The problem is that the API is very picky, it doesn't accept break points (backslash) and it doesn't accept double quotes on the beginning and on the end of the JSON string. 问题在于该API非常挑剔,它不接受断点(反斜杠),并且不接受JSON字符串开头和结尾的双引号。

So here what it's expecting: 所以这是期望的:

{
    "ConsumerToken":"aconsumertoken",
    "UserId":"email@web.com",
    "PasswordSha256Base64":"apassword"
}

And this is what I'm sending: 这就是我要发送的内容:

"{ConsumerToken:\"aconsumertoken\",UserId:\"email@web.com\",PasswordSha256Base64:\"apassword\"}"

I'm able to remove the \\" but when it sends that string it also returns an error. 我可以删除\\“,但是当它发送该字符串时,它也会返回错误。

"{ConsumerToken:aconsumertoken,UserId:email@web.com,PasswordSha256Base64:apassword}"

Is it even possible to just remove the \\ and opening and closing double quotes? 甚至可以只删除\\并以双引号和双引号引起来? And if so how? 如果是这样怎么办? I just can't seem to wrap my head around it. 我只是似乎无法绕过它。

The complete call: 完整的通话:

userhandler.cs userhandler.cs

public static object Login(string uname, string pass, string conTok)
{
    Dictionary<string, string> loginDictionary =
    new Dictionary<string, string>();
    loginDictionary.Add("ConsumerToken", conTok); 
    loginDictionary.Add("UserId", uname);
    loginDictionary.Add("PasswordSha256Base64", pass); 
    string jsonRequestString = JsonConvert.SerializeObject(loginDictionary);

    Proxy proxy = new Proxy(jsonRequestString); 
    return proxy.Execute();
}

Proxy.cs Proxy.cs

public class Proxy
{
    const string BaseUrl = "API url";
    const string ActionUrl = "API action url";

    string _jsonrequeststring; 

    public Proxy(string json)
    {
        _jsonrequeststring = json;
    }

    public Object Execute()
    {
        var client = new RestClient(BaseUrl); 
        var request = new RestRequest(ActionUrl); 

        request.RequestFormat = DataFormat.Json;
        request.AddBody(_jsonrequeststring);

        request.Method = Method.POST;
        var response = client.Execute(request);

        var content = response.Content;

        return content;
    }
}

It looks like you're adding the body content as a string, which means that it'll be encoded as a string by RestSharp. 似乎您要将主体内容添加为字符串,这意味着RestSharp会将其编码为字符串。 If possible, try adding the object itself and let RestSharp handle the serialization etc: 如果可能,请尝试添加对象本身,并让RestSharp处理序列化等:

var obj = new {
                  ConsumerToken = "aconsumertoken",
                  UserId = "email@web.com",
                  PasswordSha256Base64 = "apassword"
              };
request.AddBody(obj);

If you must use a pre-serialized JSON string for some reason, you should be able to do it like this: 如果由于某种原因必须使用预序列化的JSON字符串,则应该可以这样做:

request.AddParameter("application/json", _jsonrequeststring, ParameterType.RequestBody);

[NB: I haven't actually tested either of these suggestions, but I think they should do the trick] [NB:我实际上没有测试过这些建议中的任何一条,但我认为它们应该可以解决问题]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM