C#Extra Loop?列表被错误使用?

时间:2017-05-07 03:02:10

标签: c# .net arrays json for-loop

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;

namespace ConsoleApplication3
   {
  class Program
   {
        //Attribute properties
        public class Attribute
    {
        public string attributeUIDKey { get; set; }
        public string name { get; set; }
        public string value { get; set; }
    }
        //There are several attributes per User. Each Row of the CSV document are all the attributes for one user
        public class RootObject
    {
        public List<Attribute> attributes { get; set; }
    }

    static void Main(string[] args)
    {
        //Write the results to a txt file for testing
        StreamWriter sw = new StreamWriter("C:\\test\\testjson.txt");

        //I am initiating this wrong :(
        RootObject users = new RootObject();

        List<Attribute> csvList = new List<Attribute>();
        //Get the CSV file full of User Attributes. One row = one user
        string fPath = @"C:\test\csvTest.csv";

        string[] lines = File.ReadAllLines(fPath);

        string[] json = new string[lines.Length];

        //Make array to separate User key to make API call in separate API class
        string[] userUIDkey = new string[lines.Length];

        //Since each line is a different User, every user no matter how many is their own line. starts assigning work
        for (int l = 0; l < lines.Length;)
        {
            csvList.Clear();//I know I shouldn't need this!

            //Split each line into a string array to assign to Item. Removing any empty attributes along the way
            string[] uDComplete = lines[l].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            //Need that user Key for the API call later.
            userUIDkey[l] = uDComplete[0];

            //Begin going through each line
            for (int i = 1; i < uDComplete.Length; i += 3)
            {
                csvList.Add( new Attribute() { attributeUIDKey = uDComplete[i], name = uDComplete[i + 1], value = uDComplete[i + 2] });

                //I know this is wonky, I couldn't figure out how to intiate the RootObject, every try ending in a 'Null reference'. This works wit hthe "csvList.Clear()" 
                users.attributes= csvList;
            }
            //Due to how the API accepts the JSON payload, this serializes the array correctly, making JSON array per line
            json[l] = JsonConvert.SerializeObject(csvList);

            //writing to the test file
            sw.WriteLine(json[l]);

            //Go to next User
            l++;
        }
        sw.Close();
    }
}
}

好的,这是我从SQL查询中获取的CSV文件。我控制它的外观,这是我根据数据库设计选择的。 您可以根据图像构建自己的文件(取出列名,我不会使用它们,也不要在我的代码中删除它们),上面的代码会输出到窗口和TXT文件中我想要。 (我使用查找和替换来更改公司信息,对不起,如果我错过了一些替换资本!)

但我知道我不需要那个

csvList.Clear();

我必须投入,因为我没有弄清楚如何正确启动 RootObject类制作List<Attributes> users

2部分问题:
- 如何在For循环中动态正确启动RootObject? - 根据我的要求,有更简洁的方法来构建List吗?

(我尽可能地评论,希望我的问题/评论很清楚!谢谢你的帮助!)

0 个答案:

没有答案