如何将这样的字符串转换成数据网格?(C#Net)

如何将这样的字符串转换成数据网格?(C#Net),c#,php,arrays,string,C#,Php,Arrays,String,所以我有这样的字符串(从php服务器接收…数组的正常print\r) (来源:) 我需要知道如何在C中用这样的表表示它# (来源:) 如何做到这一点?我对php不太在行,但我认为您应该将给定的字符串安排在更类似于网格的位置(例如类似csv的文件),然后用c轻松解析它 由于您给出的字符串基本上是php,因此可以使用php解析器(php或c#中的direclty)对其进行解析,并分析可以构建网格的词条 请在此查看一些提示--> 编辑1: 无论如何,如果结构始终相同(即数组([0]=>Array(

所以我有这样的字符串(从php服务器接收…数组的正常
print\r


(来源:)

我需要知道如何在C中用这样的表表示它#


(来源:)


如何做到这一点?

我对php不太在行,但我认为您应该将给定的字符串安排在更类似于网格的位置(例如类似csv的文件),然后用c轻松解析它

由于您给出的字符串基本上是php,因此可以使用php解析器(php或c#中的direclty)对其进行解析,并分析可以构建网格的词条

请在此查看一些提示-->

编辑1: 无论如何,如果结构始终相同(即数组([0]=>Array(“内部无数组”)[1]=>Array(“内部无数组”)…),则可以使用正则表达式在c#中解析它

编辑2: 我的意思是这样的(非常粗糙但经过测试):

Regex-arrayRegex=new-Regex(@“Array[\t]*\(.+)\)”;
Regex rowRegex=newregex(@“\[([^\]]+)\]=>Array\([^]+)[\t]*\”;
Regex entryRegex=newregex(@“\[([^\]]+)\]=>([^\]\[]+]);
var rows=新列表();
var matches=arrayRegex.matches(textToParse);
foreach(匹配中的匹配)
{
如果(match.Groups.Count!=2)
抛出新异常(“无效数组”);
var rowsMactches=rowRegex.Matches(match.Groups[1].Value);
foreach(在rowsMactches中匹配rowsmatch)
{
var rowDict=new SortedDictionary();
如果(rowMatch.Groups.Count!=3)
抛出新异常(“无效行”);
var entryMatches=entryRegex.Matches(rowMatch.Groups[2].Value);
foreach(在entryMatches中匹配entryMatch)
{
if(entryMatch.Groups.Count!=3)
抛出新异常(“无效条目”);
string key=entryMatch.Groups[1]。值;
字符串val=entryMatch.Groups[2]。值;
rowDict.Add(key,val);
}
rows.Add(rowDict);
}
}
//使用第一行构建列(注意,我们假设所有字典都有相同的键)
var firstRow=rows.First();
DataTable dt=新的DataTable();
foreach(firstRow.Keys中的字符串colName)
{
dt.Columns.Add(colName);
}
foreach(行中的变量行)
{
Add(row.Values.Cast().ToArray());
}

我对php不太在行,但我认为您应该将给定的字符串安排在更类似于网格的位置(如类似csv的文件),然后用c#轻松解析它

由于您给出的字符串基本上是php,因此可以使用php解析器(php或c#中的direclty)对其进行解析,并分析可以构建网格的词条

请在此查看一些提示-->

编辑1: 无论如何,如果结构始终相同(即数组([0]=>Array(“内部无数组”)[1]=>Array(“内部无数组”)…),则可以使用正则表达式在c#中解析它

编辑2: 我的意思是这样的(非常粗糙但经过测试):

Regex-arrayRegex=new-Regex(@“Array[\t]*\(.+)\)”;
Regex rowRegex=newregex(@“\[([^\]]+)\]=>Array\([^]+)[\t]*\”;
Regex entryRegex=newregex(@“\[([^\]]+)\]=>([^\]\[]+]);
var rows=新列表();
var matches=arrayRegex.matches(textToParse);
foreach(匹配中的匹配)
{
如果(match.Groups.Count!=2)
抛出新异常(“无效数组”);
var rowsMactches=rowRegex.Matches(match.Groups[1].Value);
foreach(在rowsMactches中匹配rowsmatch)
{
var rowDict=new SortedDictionary();
如果(rowMatch.Groups.Count!=3)
抛出新异常(“无效行”);
var entryMatches=entryRegex.Matches(rowMatch.Groups[2].Value);
foreach(在entryMatches中匹配entryMatch)
{
if(entryMatch.Groups.Count!=3)
抛出新异常(“无效条目”);
string key=entryMatch.Groups[1]。值;
字符串val=entryMatch.Groups[2]。值;
rowDict.Add(key,val);
}
rows.Add(rowDict);
}
}
//使用第一行构建列(注意,我们假设所有字典都有相同的键)
var firstRow=rows.First();
DataTable dt=新的DataTable();
foreach(firstRow.Keys中的字符串colName)
{
dt.Columns.Add(colName);
}
foreach(行中的变量行)
{
Add(row.Values.Cast().ToArray());
}

我不知道是否有用,但我做了一些编辑;)我不知道是否有用,但我做了一些编辑;)
 Array ( [item_number_in_array] => Array ( [id] => id_value [title] title_value_as_string_vith_spaces [content] => content_value_as_string_vith_spaces ) [item_number_in_array]... )
Regex arrayRegex = new Regex(@"Array[ \t]*\((.+)\)");
Regex rowRegex = new Regex(@"\[([^\]]+)\] => Array \( ([^)]+)[ \t]*\)");
Regex entryRegex = new Regex(@"\[([^\]]+)\] => ([^\]\[]+)");

var rows = new List<SortedDictionary<string,string>>();
var matches = arrayRegex.Matches(textToParse);
foreach (Match match in matches)
{
    if (match.Groups.Count != 2)
        throw new Exception("Invalid array");
    var rowsMactches = rowRegex.Matches(match.Groups[1].Value);
    foreach (Match rowMatch in rowsMactches)
    {
        var rowDict = new SortedDictionary<string, string>();
        if (rowMatch.Groups.Count != 3)
            throw new Exception("Invalid row");
        var entryMatches = entryRegex.Matches(rowMatch.Groups[2].Value);
        foreach (Match entryMatch in entryMatches)
        {
            if (entryMatch.Groups.Count != 3)
                throw new Exception("Invalid entry");
            string key = entryMatch.Groups[1].Value;
            string val = entryMatch.Groups[2].Value;
            rowDict.Add(key, val);
        }
        rows.Add(rowDict);
    }
}

// use the first row to build the columns (N.B. we suppose all dictionaries have the same keys)
var firstRow = rows.First();
DataTable dt = new DataTable();
foreach (string colName in firstRow.Keys)
{
    dt.Columns.Add(colName);
}
foreach (var row in rows)
{
    dt.Rows.Add(row.Values.Cast<object>().ToArray());
}