简体   繁体   English

将字面不正确编码的字符串(例如,“ñ”)转换为 ISO-8859-1 (Latin1) H

[英]Convert a literal improperly encoded string (e.g., "ñ") to ISO-8859-1 (Latin1) H

Without going into too much detail, I have a C# WCF application that is a wrapper for an XML based API I am calling.无需过多赘述,我有一个 C# WCF 应用程序,它是我正在调用的基于 XML 的 API 的包装器。 That API returns a string, which is really just an XML document.该 API 返回一个字符串,它实际上只是一个 XML 文档。 I then parse that XML, and return it.然后我解析那个 XML,并返回它。 That returned information is displayed in the browser as JSON.返回的信息在浏览器中显示为 JSON。

A bit confusing, but here is some sampled code:有点混乱,但这里有一些示例代码:

[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json, UriTemplate = "/TestGetUser")]
TestGetUserResponse TestGetUser();

/* ... */

[DataContract(Namespace = "http://schema.mytestdomain/", Name = "TestGetUser")]
public class TestGetUserResponse
{
    [DataMember]
    public User User { get; set; }
    [DataMember]
    public Error Error { get; set; }
}

And TestGetUser being:TestGetUser是:

public TestGetUserResponse TestGetUser() {
    WebClient client = getCredentials(); // getCredentials() method is defined elsewhere

    string apiUrl = "http://my.api.url.com/API";
    string apiRequest = "<?xml version='1.0' encoding='utf-8' ?><test>My XML Request Lives Here</test>";
    
    string result = client.UploadString(apiUrl, apiRequest);
    
    XmlDocument user = new XmlDocument();
    user.LoadXml(result);
    
    userNode = user.SelectSingleNode("/my[1]/xpath[1]/user[1]");
    
    return new TestGetUserResponse {
        Error = new Error(),
        User = new User {
            Name = userNode.SelectSingleNode("name[1]").InnerText,
            Email = userNode.SelectSingleNode("email[1]").InnerText,
            ID = System.Convert.ToInt32(userNode.SelectSingleNode("id[1]").InnerText)
        }
    };
}

So, when I hit my URL from a browser, it returns a JSON string, like below:因此,当我从浏览器点击我的 URL 时,它会返回一个 JSON 字符串,如下所示:

{
    "Error": {
        "ErrorCode": 0,
        "ErrorDetail": null,
        "ErrorMessage":"Success"
    },
    "User": {
        "Name": "John Smith",
        "Email": "john.smith@example.com",
        "ID": 12345
    }
}

Now, my problem is, sometimes the string that is returned (directly from the API) is a badly encoded UTF-8 string (I think? I could be getting this a bit wrong).现在,我的问题是,有时返回的字符串(直接从 API 返回)是编码错误的 UTF-8 字符串(我认为?我可能会弄错)。 For example, I may get back:例如,我可能会回来:

{
    "Error": {
        "ErrorCode": 0,
        "ErrorDetail": null,
        "ErrorMessage":"Success"
    },
    "User": {
        "Name": "Jose Nuñez",
        "Email": "jose.nunez@example.com",
        "ID": 54321
    }
}

Notice the ñ in the Name property under the User object.请注意 User 对象下 Name 属性中的ñ

My question is, how can I convert this improperly encoded string to a ñ , which is what it should be?我的问题是,如何将这个编码不当的字符串转换为ñ ,它应该是什么?

I've found a bunch of posts找了一堆帖子

But none seem to be exactly what I need, or trying to borrow from those posts have failed.但似乎没有一个正是我需要的,或者试图从这些帖子中借用失败。

So, to make my question as simple as possible,所以,为了让我的问题尽可能简单,

If I have a variable in a C# (.NET 3.5) application that when I write it out to the screen get's written as 'ñ', how can I "re-encode" (may be wrong word) so that it outputs as 'ñ'?如果我在 C# (.NET 3.5) 应用程序中有一个变量,当我将它写到屏幕上时,它会被写为“ñ”,我如何“重新编码”(可能是错误的词)以便它输出为'ñ'?

Thanks in advance.提前致谢。

Ideally this would be fixed in the api you are calling so it is returning the expected encoding.理想情况下,这将在您正在调用的 api 中修复,以便它返回预期的编码。 But you should be able to fix it this way:但是你应该能够通过这种方式修复它:

byte[] bytes = Encoding.GetEncoding(1252).GetBytes(Name);
var nameFixed = Encoding.UTF8.GetString(bytes);

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

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