I'm developing a program, I know how to de-serialize a JSON. But I don't know how to de-serialize it when it's just one string in quotes.
When I request the JSON, and put it into a string, then write it to the console.
I get this:
How do I remove those quotes so it's just a normal string?
The API Documentation has this:
Response
Returns 201 Created if succeeds, 401 Unauthorized otherwise.
Returns user's authentication token as a JSON string (i.e. in double quotes). This token must be used as the auth_token parameter for methods that require authentication.
For the auth_token to work I need to remove the double quotes from that string.
Now, this may not have anything to do with JSon as I see now.
How do I remove those double quotes?
How do I remove those double quotes?
You don't want to. You want to parse it. You do that like you do with any JSON, using NewtonSoft.Json:
var token = JsonConvert.DeserializeObject<string>("\"foo\"");
Yields the string foo.
Related
I am working on a small application based on owin and katana to handle links internally.
So the application handles HttpGet requests. When someone calls
http:localhost/?document=path/to/my/document/foo.doc
the application opens this document.
My problem is: When the document name contains a special character like '+' my code interprets the + sign as space because the variable is parsed into a string.
[HttpGet]
[Route("")]
public HttpResponseMessage Get(string document = "")
{
//open document
}
So how to preserve the special characters and don't allow c# string to convert them before executing any code?
I tried with HttpResponseMessage Get([FromUri]string document = "")
I tried encoding the document variable afterwards with HttpUtility.UrlEncode but it will also encode the legit spaces.
Meanwhile I got a workaround:
this.Request.RequestUri.OriginalString
inside of the GET Method will give the the full link as string without any interpretations. The rest is to get the relevant variable with string.substring operations.
Nevertheless it would like to know if someone knows a more elegant solution.
I have a Web Api project and a controller receives a Json parameter like this
public HttpResponseMessage QueryRead([System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))] DataSourceRequest request, string queryparams, int qryId)
The string queryparams is actually a Json object that the client sends in the form
"{\"Supplier\":{\"name\":\"Supplier\",\"type\":\"PK\",\"textvalue\":\"[{\"Supplier_Key\":2,\"Supplier_Value\":\"Foxes Beverages \"}]\",\"value\":\"[2]\"}}"
This is a valid json object.
The problem is that the string queryparams removes all backslashes and the string becomes
{"Supplier":{"name":"Supplier","type":"PK","textvalue":"[{"Supplier_Key":2,"Supplier_Value":"Foxes Beverages "}]","value":"[2]"}}
It ommits all backslashes and the later is not a valid json
Any ideas?
If your client is really sending you
"{\"Supplier\":{\"name\":\"Supplier\",\"type\":\"PK\",\"textvalue\":\"[{\"Supplier_Key\":2,\"Supplier_Value\":\"Foxes Beverages \"}]\",\"value\":\"[2]\"}}"
...the trouble is at the client end. It looks like double-encoded JSON, as though they'd done this (in JavaScript):
var json = JSON.stringify(originalObject);
var json2 = JSON.stringify(json);
...but the textvalue part is wrong even for double-encoded JSON, so it's not that simple. So if (again) they're really sending that to you, the problem is at their end. Moreover, you can't correct it. If it were "just" double-encoded JSON, you could correct for it by parsing it twice. But it isn't, because of the value of textvalue, so it's nonsense (if it's really like that).
I've to return an xml string in the response of a WCF method. I've found that the parser sometimes encode the string (e.g <test>) while sometimes it wraps all the string into a CDATA (e.g. <![CDATA[<test>]]>).
In particular I've noticed that for strings in which there are a small number of escape characters it uses the first method otherwise the second.
I need to have all the string in the first format, and not in the CDATA. Is there a way to accomplish that?
I've tried to encode the string myself before the response, but then it tries to encode the character "&" and so nothing changes.
I have a literal string generated by my bussiness logic that I need to send out on my web api via my controller.
The end part of my controller function looks like this, where the text variable is a literal string, thus containing "\\" to indicate a single backslash:
var text = _transformation.ToTextFormula(new Formula("", formula, parts));
return Ok(text);
The problem this creates is that when I then consume my api the duble backslashes are still there and not just the single one intended. Surely there must be a way to correct what is sent out?
If I inspect the "text" variable to look at the value in real format there is just a single slash before leaving the method.
I wanna send JSON response to browser that requested based on REST service. I use something like this that includes some quotes in Controller Method:
return Json("blah\"blah", JsonRequestBehavior.AllowGet);
And I expect the result would be blah"blah But is blah\"blah and includes back slash too! I wanna have blah"blah in response without any conversion in client side. I know that need to perform this via C# codes but how to do that?
C# and JSON encode characters similarly. The string blah"blah is encoded in both C# and JSON as "blah\"blah". It's perfectly expected, then, that your raw JSON includes the backslash.
When you decode that string with a proper JSON library, it again becomes the string blah"blah.
I found the answer in two above threads:
ASP.Net MVC: how to create a JsonResult based on raw Json Data
How to output Json string as JsonResult in MVC4?
So that I need to use something like this:
return Content(jsonStringFromSomewhere, "application/json");
With this in mind considering that JSONP is used in case of ajax requesting to external service or URL. But I wanna build specific string due to parse with a special parser and I used JSON rather than JSONP and result was great.