Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件解析为列表_C#_Php_Arrays_List_Parsing - Fatal编程技术网

C# 将文件解析为列表

C# 将文件解析为列表,c#,php,arrays,list,parsing,C#,Php,Arrays,List,Parsing,我正在用C#将一个文件(php)解析为一个列表,这样我就可以处理它了。我以前在PHP中做过,但在那里它的工作速度非常慢,所以我正在尝试用它制作一个C#winform应用程序 这是我在C#中已有的代码: 我有一些PHP代码如下: array('uid' => '1','pid' => '2','keyword_uid' => '1','product_uid' => '1','amount' => '15','in_title' => '0'), array('

我正在用C#将一个文件(php)解析为一个列表,这样我就可以处理它了。我以前在PHP中做过,但在那里它的工作速度非常慢,所以我正在尝试用它制作一个C#winform应用程序

这是我在C#中已有的代码:

我有一些PHP代码如下:

array('uid' => '1','pid' => '2','keyword_uid' => '1','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '2','pid' => '2','keyword_uid' => '2','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '3','pid' => '2','keyword_uid' => '3','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '4','pid' => '2','keyword_uid' => '4','product_uid' => '1','amount' => '9','in_title' => '0'),
array('uid' => '5','pid' => '2','keyword_uid' => '5','product_uid' => '1','amount' => '18','in_title' => '0'),
array('uid' => '6','pid' => '2','keyword_uid' => '6','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '7','pid' => '2','keyword_uid' => '7','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '8','pid' => '2','keyword_uid' => '8','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '9','pid' => '2','keyword_uid' => '9','product_uid' => '1','amount' => '79','in_title' => '1'),
array('uid' => '10','pid' => '2','keyword_uid' => '10','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '11','pid' => '2','keyword_uid' => '11','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '12','pid' => '2','keyword_uid' => '12','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '13','pid' => '2','keyword_uid' => '13','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '14','pid' => '2','keyword_uid' => '14','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '15','pid' => '2','keyword_uid' => '15','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '16','pid' => '2','keyword_uid' => '16','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '17','pid' => '2','keyword_uid' => '17','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '18','pid' => '2','keyword_uid' => '18','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '19','pid' => '2','keyword_uid' => '19','product_uid' => '1','amount' => '15','in_title' => '1'),
array('uid' => '20','pid' => '2','keyword_uid' => '20','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '21','pid' => '2','keyword_uid' => '21','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '22','pid' => '2','keyword_uid' => '22','product_uid' => '1','amount' => '21','in_title' => '0'),
array('uid' => '23','pid' => '2','keyword_uid' => '23','product_uid' => '1','amount' => '12','in_title' => '0'),
require_once 'export/tx_ttproductsv2_keywords_in_products_part.php';
$sql = new Sql;
    $tagList = array();
    $counter = 0;
    $counter2 = 0;
    $counter3 = 0;

    foreach ($tx_ttproductsv2_keywords_in_products as $key => $value) {
        $keywordsList = $sql -> getKeyWords($value['keyword_uid']);
        foreach ($keywordsList as $key2 => $value2) {
            if(isset($tagList[$value['product_uid']])){
                $tagList[$value['product_uid']] .= ", " . $value2['keyword'];
            }else{
                $tagList[$value['product_uid']] = $value2['keyword'];
            }
        }
    }
通过这种方式(在PHP中),我检查数组中的一个条目是否已经是exist,如果是真的,则追加,或者创建一个新条目


我的问题是:我如何在C#中做到这一点。我想我必须使用一个列表,但我真的不知道具体如何使用。

我编写了一个快速而肮脏的控制台程序,将其转换为数组。看看你是否可以根据自己的目的修改它。。。如果没有,请更详细地解释您需要它做什么

(注意:这是一项快速的工作,不是最有效的处理方式。)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
名称空间SimpleRayThing
{
类myarrayitem
{
公共信息;
公共int pid;
公共int关键字\u uid;
公共int产品uid;
公共整数金额;
公共int in_标题;
}
班级计划
{
静态void Main(字符串[]参数)
{
//数组('uid'=>'23','pid'=>'2','keyword\u uid'=>'23','product\u uid'=>'1','amount'=>'12','in\u title'=>'0'),
字符串Original=File.ReadAllText(“array.txt”).Replace(“array('uid'=>”,“”).Replace(
“,'pid'=>”,“/”。替换(“,'keyword\u uid'=>”,“/”。替换(“,'product\u uid'=>”,“/”)。替换(
“,”金额“=>”,“/”。替换(“,”在标题“=>”,“/”)中。替换(“),”,”)。替换(“\r”,”);
字符串[]行=原始.Split('\n');
List mystuff=新列表();
对于(int i=0;i
针对评论:

List<myarrayitem> items new List<myarrayitem>();
for (int i = 0;i < mystuff.Count;i++)
{
      if (mystuff[i].product_uid == PRODUCT_UID_HERE)
      {
            items.add(mystuff[i]);
            // Or, instead of this items List, you could just do whatever you wanted to do with the items right here.
      }
}
List items new List();
for(int i=0;i
试试这个:

  • 得到
  • 将以下类添加到代码中:
  • 将文件加载到名为toProcess的字符串中,然后运行以下操作:
  • //接下来的4行将数据转换为JSON格式
    toProcess=toProcess.Replace(“数组(“,”{\“DataArray\”:{”);
    toProcess=toProcess.Replace(“)”,“}”)
    toProcess=toProcess.Replace(“'”,“\”);
    toProcess=“[”+toProcess.Replace(“=>”,“:”+“])

    List data=JsonConvert.DeserializeObject(toProcess);
    foreach(数据中的RootObject d){
    //在这里做点像。。
    如果(d.DataArray.uid==“1”){
    //
    }
    }
    

    Hy,谢谢你的回答。你为什么要投否决票?因为我觉得这很好?@Mathlight:谁知道呢。有些否决票只是来自其他人,他们的回答只是希望保持他们的答案在顶部。不知道这里是否是这样,但不管怎样。看起来很好。有了它,我可以在sa获得具有相同产品ID的所有项目吗我的时间?@Mathlight当然!我用一些示例代码编辑了我的回复帖子,这些代码是你可以做到的。这很简单……但是这种方法不会产生巨大的开销吗?因为我有数千种产品,而且每种产品我都必须滚动到列表???与仅仅阅读列表所需的开销相比,它产生的开销非常小来自该文件的数据,而不是内部存储。哦,但是你也可以为每个产品或其他东西制作列表。这只是一次性的电力浪费,这样可以节省电力。(例如,创建一个项目列表。第一个列表中的索引是产品uid…该索引中的子列表是该ID下的所有产品)我想我会把最后一个建议弄清楚的。无论如何,谢谢你的代码和想法;)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace simplearraything
    {
    class myarrayitem
    {
        public int uid;
        public int pid;
        public int keyword_uid;
        public int product_uid;
        public int amount;
        public int in_title;
    }
    class Program
    {
        static void Main(string[] args)
        {
            //array('uid' => '23','pid' => '2','keyword_uid' => '23','product_uid' => '1','amount' => '12','in_title' => '0'),
            string Original = File.ReadAllText("array.txt").Replace("array('uid' => '", "").Replace(
                "','pid' => '", "/").Replace("','keyword_uid' => '", "/").Replace("','product_uid' => '", "/").Replace(
                "','amount' => '", "/").Replace("','in_title' => '", "/").Replace("'),", "").Replace("\r","");
            string[] lines = Original.Split('\n');
            List<myarrayitem> mystuff = new List<myarrayitem>();
            for (int i = 0; i < lines.Length; i++)
            {
                string[] thisitem = lines[i].Split('/');
                myarrayitem item = new myarrayitem();
                item.uid = int.Parse(thisitem[0]);
                item.pid = int.Parse(thisitem[1]);
                item.keyword_uid = int.Parse(thisitem[2]);
                item.product_uid = int.Parse(thisitem[3]);
                item.amount = int.Parse(thisitem[4]);
                item.in_title = int.Parse(thisitem[5]);
                mystuff.Add(item);
            }
    
    
            // Now, output it to prove accuracy:
            for (int i = 0; i < lines.Length; i++)
            {
                Console.WriteLine("UID:" + mystuff[i].uid.ToString() + "/PID:" + mystuff[i].pid.ToString() + "/KEYWORD_UID:" + mystuff[i].keyword_uid.ToString() +
                    "/PRODUCT_UID:" + mystuff[i].product_uid.ToString() + "/amount:" + mystuff[i].amount.ToString() + "/in_title:" + mystuff[i].in_title.ToString());
            }
            Console.Read();
        }
    }
    }
    
    List<myarrayitem> items new List<myarrayitem>();
    for (int i = 0;i < mystuff.Count;i++)
    {
          if (mystuff[i].product_uid == PRODUCT_UID_HERE)
          {
                items.add(mystuff[i]);
                // Or, instead of this items List, you could just do whatever you wanted to do with the items right here.
          }
    }
    
    public class DataArray {
      public string uid { get; set; }
      public string pid { get; set; }
      public string keyword_uid { get; set; }
      public string product_uid { get; set; }
      public string amount { get; set; }
      public string in_title { get; set; }
    }
    
    public class RootObject {
      public DataArray DataArray { get; set; }
    }
    
    List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(toProcess); 
    
    foreach ( RootObject d in data ) {    
      // do something here like..
      if (d.DataArray.uid == "1") {
          //
      }
    }