How do I remove backslashes from URLs? - c#

I am using the Twitter API, and the response I get is in JSON format. When I parse JSON using C# so the value of profile_image_url does not contain the proper URL. This URL is absolutly fine in the response, but after parsing the response I get the following URL. How do I remove backslashes?
http://a1.twimg.com/profile_images/700049686/14_normal.jpg

You can remove backslashes using the Replace function:
url = url.Replace("\\", "")
But perhaps you ought to spend some time working out how those backslashes got there in the first place. It sounds like you aren't parsing the JSON correctly. What parser are you using?

Related

c# clean URL string from API

Im calling an API that returns part of a URL that I have to concatenate on code after.
The value im getting is:
"\"SYSTEM\/PRODUCTION\/TEMP\/preauthletter_7470D75C-594D-4132-BAEE-F7339B034DD8_110220201138.pdf\""
When I try to concatenate to my base URL the result is an invalid URL because of the previews extras slash and quotes.
var result = _reportEngine.GetPreAuthorizationLetter().Result;
// result is the URL part having the extra slashes
result = string.Format(#"http://cloud.demo.com/{0}", result);
Is there any .NET method that cleans this so I can get a valid URL.
The result expected should be:
http://cloud.demo.com/SYSTEM/PRODUCTION/TEMP/preauthletter_7470D75C-594D-4132-BAEE-F7339B034DD8_110220201138.pdf
UPDATE
The problem I guess is that API is returning as json instead of just pure string.
Found how to solve it, I just had to deserialize it into a string:
result = JsonConvert.DeserializeObject<string>(result);
And voila!!

Corrupted JSON HTTP response

I am getting a HTTP request for a website and the content type is JSON. However, I am getting a nested JSON that is a unicode and is causing consistency problems.
Here is an example:
{"key1":"value",
"key2":"value",
"key3":{
u'key31':u'value',
u'key32':u'value'}}
This reminds me of python 2.7 troubles but I am not sure how to fix this JSON. I am using C# to parse it. Everything works correctly until I try to access key3.
The content should be a JSON object type but it is considered rather a value or a string.
Thanks for ya help. Is there a way to fix it if it is actually corrupted or am I parsing it wrongly?
You're correct that this json object is not complete / does not have the correct syntax. You're missing a closing '}' character.
How are you parsing your data? Try taking a look at this documentation.
your json object is not in valid formatted it should be like as folllows
{
"key1":"value",
"key2":"value",
"key3":{
" u'key31'":"u'value'",
"u'key32'":"u'value'"
}
}
by any chance do you get this json from python dump? coz Python's unicode literals are not valid JSON, and neither are single quotes

Convert HTML / Java encoded diactric signs from JSON message

I receive form API a JSON message which looks like:
{"message":"Nieprawid\u0142owy format"}
"\u0142" is polish charakter "ł". How can I convert the whole JSON message, as it may contain much more signs encoded this way? I have tried HTMLDecode, URLDecode and few others but none worked.
I try do to this with c# and asp.net. Thanks for any ideas.
A JSON parser will take care of the conversion:
var json = (JObject)JsonConvert.DeserializeObject(str);

converting json string into json on client side?

In my javascript code I am getting json string from cs file
var tmpString="<%=resultset2%>";
In cs I am concatenating strings to build json string. Here is an issue the json string is returned as a string and it has " with it.
"[{id:'1',name:'Aik'},{id:'2',name:'Aik or Aik'}]"
Because of " in beginning and end javascript code treat it as a string. Kindly guide me how I should sort out this issue.
thanks
Fix the JSON, it has errors (property names must be strings (and thus quoted), and only " are acceptable for quoting strings in JSON). JSON is a subset of JavaScript, you can't use all of JS' syntax in JSON. As a rule of thumb, if you are concatenating strings to produce a data format, then you are doing it wrong. http://json.org/ lists a number of C# libraries that you can use to build JSON.
Use json2.js
Change this:
var tmpString="<%=resultset2%>";
to:
var tmpString=<%=resultset2%>;
This isn't JSON, you're just writing javascript from a server page. The problem is you are creating invalid javascript syntax, you just need to remove the quotes.
The quotes aren't from resultset2 they are from your markup.

RSS Feed validation: IRI found where URL expected - How to convert IRI link to the valid URL?

I have created an RSS feed, that contains non-ASCII characters URLs (IRI). That's why it does not validate using Feed validator.
How important is that validation? As far as I know this is more or less a validation for legacy RSS readers?
Anyway, I would like to know how to convert IRI to URL in C#?
http://www.viva.si/Zdrav-način-prehrane/204/Jagodičje
to
http://www.viva.si/Zdrav-na%C4%8Din-prehrane/204/Jagodi%C4%8Dje
Solution:
string url = new Uri(iri).AbsoluteUri

Categories