C#额外循环?列表使用不正确? 使用系统; 使用System.Collections.Generic; 使用Newtonsoft.Json; 使用System.IO; 命名空间控制台应用程序3 { 班级计划 { //属性属性 公共类属性 { 公共字符串attributeUIDKey{get;set;} 公共字符串名称{get;set;} 公共字符串值{get;set;} } //每个用户有几个属性。CSV文档的每一行都是一个用户的所有属性 公共类根对象 { 公共列表属性{get;set;} } 静态void Main(字符串[]参数) { //将结果写入txt文件进行测试 StreamWriter sw=新的StreamWriter(“C:\\test\\testjson.txt”); //我是错的:( RootObject users=新的RootObject(); List csvList=新列表(); //获取包含用户属性的CSV文件。一行=一个用户 字符串fPath=@“C:\test\csvTest.csv”; string[]lines=File.ReadAllLines(fPath); string[]json=新字符串[lines.Length]; //使数组分离用户密钥,在单独的API类中进行API调用 string[]userUIDkey=新字符串[lines.Length]; //因为每一行都是不同的用户,所以每个用户无论有多少行都是他们自己的。开始分配工作 对于(int l=0;l
好的,这是我从SQL查询中生成的CSV文件。我控制它的外观,这是我根据我们的DB设计选择的。 您可以根据图像构建自己的文件(去掉列名,我不会在代码中使用它们,也不会删除它们),上面的代码将输出到窗口和TXT文件中,这正是我想要的。(我使用查找和替换来更改公司信息,如果我遗漏了一些替换的大写形式,请原谅!) 但我知道我不需要这个C#额外循环?列表使用不正确? 使用系统; 使用System.Collections.Generic; 使用Newtonsoft.Json; 使用System.IO; 命名空间控制台应用程序3 { 班级计划 { //属性属性 公共类属性 { 公共字符串attributeUIDKey{get;set;} 公共字符串名称{get;set;} 公共字符串值{get;set;} } //每个用户有几个属性。CSV文档的每一行都是一个用户的所有属性 公共类根对象 { 公共列表属性{get;set;} } 静态void Main(字符串[]参数) { //将结果写入txt文件进行测试 StreamWriter sw=新的StreamWriter(“C:\\test\\testjson.txt”); //我是错的:( RootObject users=新的RootObject(); List csvList=新列表(); //获取包含用户属性的CSV文件。一行=一个用户 字符串fPath=@“C:\test\csvTest.csv”; string[]lines=File.ReadAllLines(fPath); string[]json=新字符串[lines.Length]; //使数组分离用户密钥,在单独的API类中进行API调用 string[]userUIDkey=新字符串[lines.Length]; //因为每一行都是不同的用户,所以每个用户无论有多少行都是他们自己的。开始分配工作 对于(int l=0;l,c#,.net,arrays,json,for-loop,C#,.net,Arrays,Json,For Loop,好的,这是我从SQL查询中生成的CSV文件。我控制它的外观,这是我根据我们的DB设计选择的。 您可以根据图像构建自己的文件(去掉列名,我不会在代码中使用它们,也不会删除它们),上面的代码将输出到窗口和TXT文件中,这正是我想要的。(我使用查找和替换来更改公司信息,如果我遗漏了一些替换的大写形式,请原谅!) 但我知道我不需要这个 csvList.Clear(); 因为我不知道如何正确启动 RootObject类创建一个列表用户 2部分问题: --如何在For循环中正确地动态启动RootObject
csvList.Clear();
因为我不知道如何正确启动
RootObject
类创建一个列表用户
2部分问题:--如何在For循环中正确地动态启动RootObject?
--有没有一种更干净的方法可以根据我的需求建立一个列表 (我尽可能地发表了评论,希望我的问题/评论都很清楚!谢谢你的帮助!)
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();
}
}
}