【问题标题】:parse a string with name-value pairs解析具有名称-值对的字符串
【发布时间】:2012-07-26 16:23:16
【问题描述】:

如何在 C# 中解析以下名称-值对字符串:

string studentDetail = "StudentId=J1123,FirstName=Jack,LastName=Welch,StudentId=k3342,FirstName=Steve,LastName=Smith"

解析这个数组的目的是使用Linq to SQL在DB中插入值:

[HttpPost]
public ActionResult SaveStudent(string studentDetail)
{
    DataContext db = new DataContext();         

    Student student = new Student();
    {
        student.StudentID = //StudentID
        student.FirstName = //FirstName
        student.LastName = //LastName
    };

    db.Student.InsertOnSubmit(student);
    db.SubmitChanges();

    return View();
}

解决这个问题的最佳方法是什么?

【问题讨论】:

  • 看起来仍然只是一个字符串,而不是字符串[]
  • 仍然不是字符串[]。也许你的意思是: var array = new string[] { "StudentId=J1123,FirstName=Jack,LastName=Welch", "StudentId=k3342,FirstName=Steve,LastName=Smith" };
  • @Jonathan 我收到的格式如下:“StudentId=J1123,FirstName=Jack,LastName=Welch,StudentId=k3342,FirstName=Steve,LastName=Smith"
  • @user793468 他们都在告诉你这不是一个字符串数组,它只是一个 [single] 字符串。

标签: c# asp.net-mvc parsing linq-to-sql


【解决方案1】:

您可以在逗号上拆分,然后在等号上拆分。我将数据放入字典以便于访问。

string input = "StudentId=J1123,FirstName=Jack,LastName=Welch";

Dictionary<string,string> keyValuePairs = input.Split(',')
  .Select(value => value.Split('='))
  .ToDictionary(pair => pair[0], pair => pair[1]);

string studentId = keyValuePairs["StudentId"];

请注意,这根本不是验证输入以确保值中没有逗号、没有没有值的键、缺少键等。

【讨论】:

  • 一直以来,我从来不知道 ToDictionary 有一个重载,它占用了键和值。
  • 很好的答案,你能指出 KeyValuePair 而不是字符串的解决方案吗? (或元组)??我有重复的值,我需要用可用的重复值解析我的字符串。
  • 由于键在字符串中多次出现而导致重复键异常。
  • 此答案不适用于问题中的原始数据。 System.ArgumentException: 'An item with the same key has already been added. Key: StudentId'
【解决方案2】:

因为个人学生记录没有在输入中分隔,所以我会执行以下操作:

public class Student
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
} 

然后:

private List<Student> DoSplit(string input)
{
    var theReturn = new List<Student>();
    input = input.Replace(",StudentId=", "|,StudentId=");

    var students = input.Split('|');

    foreach (var student in students)
    {
        var attribs = student.Split(',');
        if (attribs.Count() == 3)
        {
            var s = new Student();
            s.Id = attribs[0].Substring(attribs[0].LastIndexOf('='));
            s.FirstName = attribs[1].Substring(attribs[1].LastIndexOf('='));
            s.LastName = attribs[2].Substring(attribs[2].LastIndexOf('='));

            theReturn.Add(s);
        }
    }

    return theReturn;
}

同样,这有点幼稚,因为如果内容包含“=”、“”或“|”,则会出现故障。您还应该在那里添加一些检查。

【讨论】:

  • 这是正确答案,实际上回答了 OPs 的问题。
【解决方案3】:

Eric Petroelje 在https://stackoverflow.com/a/2049079/59996 得到了很好的回答

试试System.Web.HttpUtility.ParseQueryString,把所有东西都传进去 问号之后。您需要使用 System.Web 程序集,但它不应该需要网络上下文。

(我不确定为什么这两个问题没有关联)

我能够使用以下代码解析格式为 a=1&b=2&c=3 的字符串

NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(submission);

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多