UTF-8字符串看起来不正确

时间:2013-04-12 12:48:27

标签: c# asp.net

我有一些来自API的字符串。

api给了我这样的字符串:

enter image description here

但我怀疑我需要以某种方式制作这些UTF-16? 有没有办法用.NET做到这一点?我知道.NET更喜欢使用UTF-16 ......

public Task ParseTask(XElement task, bool singleTask)
{
    Task t = new Task();
    int id;
    int.TryParse(task.Attribute("id").Value, out id);
    t.ID = id;
    t.Title = singleTask ? task.Element("name").Value : task.Attribute("name").Value;
    t.IsComplete = task.Attribute("complete").Value == "1";
    t.IsBillable = task.Attribute("billable").Value == "1";

    if (task.Element("user").Attribute("id") != null)
    {
        t.UserID = task.Element("user").Attribute("id").Value;
        t.UserName = task.Element("user").Attribute("name").Value;
    }

    t.Description = task.Element("description").Value;
    decimal pph;
    decimal.TryParse(task.Element("price_per_hour").Value, out pph);
    t.PricePerHour = pph;

    decimal bh;
    decimal.TryParse(task.Element("budget_hours").Value, out bh);
    t.BudgetHours = bh;
    int pid;
    int.TryParse(task.Element("project").Attribute("id").Value, out pid);
    t.ProjectID = pid;

    int tlid;
    int.TryParse(task.Element("tasklist").Attribute("id").Value, out tlid);
    t.TaskListID = tlid;

    return t;
}

-

 public List<Task> GetProjectTasks(int projectID)
        {
            List<Task> list = new List<Task>();

            string args = String.Format("project_id={0}", projectID);
            string uri = CreateURI("paymo.tasks.findByProject", args);
            string reply = client.DownloadString(uri);
            XDocument doc = XDocument.Parse(reply);
            if (IsOk(doc))
            {
                IEnumerable<XElement> tasks =
                    doc.Element("response").Element("tasks").Elements();

                foreach (var task in tasks)
                {
                    list.Add(ParseTask(task, false));
                }
            }
            else
            {
                throw new Exception();
            }



            return list;
        }

1 个答案:

答案 0 :(得分:0)

.NET默认使用UTF-16字符串。函数System.Text.Encoding.UTF8.GetString()。 可以通过将数据作为Byte[]数组传入并检索UTF-16字符串来帮助您。

请参阅:Encoding.GetString Method (Byte[])