views:

321

answers:

3

Hi! I connect to a webservice that gives me a response something like this(This is not the whole string, but you get the idea):

sResponse = "{\"Name\":\" Bod\u00f8\",\"homePage\":\"http:\/\/www.example.com\"}";

As you can see, the "Bod\u00f8" is not as it should be. Therefor i tried to convert the unicode (\u00f8) to char by doing this with the string:

     public string unicodeToChar(string sString)
     {
         StringBuilder sb = new StringBuilder();
         foreach (char chars in sString)
         {
             if (chars >= 32 && chars <= 255)
             {
                 sb.Append(chars);
             }
             else
             {
                 // Replacement character
                 sb.Append((char)chars);                  
             }

         }
         sString = sb.ToString();
        return sString;
    }

But it won't work, probably because the string is shown as \u00f8, and not \u00f8.

Now it would not be a problem if \u00f8 was the only unicode i had to convert, but i got many more of the unicodes. That means that i can't just use the replace function :(

Hope someone can help.

A: 

Perhaps you want to try:

string character = Encoding.UTF8.GetString(chars);
sb.Append(character);
Will Marcouiller
nah, since the function was wrong, this will probably not work.The function takes each character, and therefor does not process the \u00f8
+1  A: 

You're basically talking about converting from JSON (JavaScript Object Notation). Try this link--near the bottom you'll see a list of publicly available libraries, including some in C#, that might do what you need.

catfood
The Json is already beeing parsed by myself, i only need to solve this problem :POr do you know of a library that kan parse this json to an array:{\"Topic\":{\"001\":{\"ItemHeader\":\"Test\",\"ItemText\":\"Comment\"}},\"Image\":{\"001\":{\"ItemImage\":\"www.image.com\",\"ImageDescription\":\"description\"}}}If you do, please leave an example to, please :)
+1  A: 

The excellent Json.NET library has no problems decoding unicode escape sequences:

var sResponse = "{\"Name\":\"Bod\u00f8\",\"homePage\":\"http://www.ex.com\"}";
var obj = (JObject)JsonConvert.DeserializeObject(sResponse);
var name = ((JValue)obj["Name"]).Value;
var homePage = ((JValue)obj["homePage"]).Value;
Debug.Assert(Equals(name, "Bodø"));
Debug.Assert(Equals(homePage, "http://www.ex.com"));

This also allows you to deserialize to real POCO objects, making the code even cleaner (although less dynamic).

var obj = JsonConvert.DeserializeObject<Response>(sResponse);
Debug.Assert(obj2.Name == "Bodø");
Debug.Assert(obj2.HomePage == "http://www.ex.com");

public class Response
{
    public string Name { get; set; }
    public string HomePage { get; set; }
}
Nathan Baulch
Sounds great, but when i have tried to add the JSON.net library earlier it has taken all the memory on my device.And one more thing that i see i have written wrong.The problem with unicode was that the string was like this:var sResponse = "{\"Name\":\"Bod\\u00f8\",....etc }"Notice that after "Bod" it is two slashes, not one. That is my real problem. And i can't just replace the \\u00f8 with \u00f8, because there will be more unicodes in my string.