using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
namespace weiCmd
{
public class WeiJson
{
/// <summary>
/// 读取带表头的csv文件生成JSON字符串
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public string WeiCsv2JSON(string file)
{
// 定义待返回的json字符串
string weiJSONstr = "";
// 加载csv文件对象
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.Default);
// 读取第一行作为Key
string[] tou = sr.ReadLine().Split(',');
// 读取后续行作为Json值
string hangStr;
weiJSONstr += "[";
while ((hangStr = sr.ReadLine()) != null)
{
string[] hang = hangStr.Split(',');
for (int i = 0; i < tou.Length; i++)
{
if (i == 0)
{
weiJSONstr += "{\"" + tou[i] + "\":\"" + hang[i] + "\",";
}
else if (i == tou.Length - 1)
{
weiJSONstr += "\"" + tou[i] + "\":\"" + hang[i] + "\"}," +
"";
}
else
{
weiJSONstr += "\"" + tou[i] + "\":\"" + hang[i] + "\",";
}
}
}
weiJSONstr = weiJSONstr.Substring(0, weiJSONstr.Length - 1) + "]";
// 关闭文件对象
sr.Close();
Console.WriteLine("打印csv文件转换成的json字符串------------------------------------");
Console.WriteLine(weiJSONstr);
//Console.ReadLine();
return weiJSONstr;
}
/// <summary>
/// 对list列表包装lmessage和WeiJson关键字,形成新的json字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string WeiCsv2JsonMessageWeiJson(string str)
{
string s;
s = "{ \"message\":\"一个JSON字段的值\",\"WeiJson\":";
s = s + str + "}";
Console.WriteLine("打印包装lmessage和WeiJson关键子的JSON字符串------------------------------------");
Console.WriteLine(s);
return s;
}
/// <summary>
/// 读取json字符串放回json结构体
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public WeiJsonStruct JsonStr2Struct(string str)
{
//解析json字符串 反序列化 方法一 需要litJson包
WeiJsonStruct WeiJsonObject = JsonMapper.ToObject<WeiJsonStruct>(str);
List<WeiHangType> list = WeiJsonObject.WeiJson;
Console.WriteLine(list[0].name + "----------");
return WeiJsonObject;
//解析json字符串 反序列化 方法二 需要Newtonsoft.Json包
//WeiJsonStruct WeiJsonObject = JsonConvert.DeserializeObject<WeiJsonStruct>(str);
//List<WeiHangType> list = WeiJsonObject.WeiJson;
//Console.WriteLine(list[0].name + "----------");
//return WeiJsonObject;
}
/// <summary>
/// JSON结构体转json字符串
/// </summary>
/// <param name="WeiJsonObject"></param>
/// <returns></returns>
public string WeiStruct2JsonStr(WeiJsonStruct WeiJsonObject)
{
string s = "";
s = JsonMapper.ToJson(WeiJsonObject);
Console.WriteLine("打印从结构体转成的字符串----------------------------");
Console.WriteLine(s);
return s;
}
/// <summary>
/// JSON结构体转csv文件
/// </summary>
/// <param name="WeiJsonObject"></param>
/// <returns></returns>
public string WeiStruct2csvStr(WeiJsonStruct WeiJsonObject)
{
string s = "";
//第一行表头
s = "name,age,work01,work02,work03,work04,work05,work06\n";
//数据行
foreach (WeiHangType hang in WeiJsonObject.WeiJson)
{
s += hang.name + "," + hang.age + "," + hang.work01 + "," + hang.work02 + "," + hang.work03 + "," + hang.work04 + "," + hang.work05 + "," + hang.work06 + "\n";
}
Console.WriteLine("打印由json结构体生成的csv文件字符串----------------------------------------");
Console.WriteLine(s);
return s;
}
/// <summary>
/// 读取文件 生成字符串
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public string ReadFileToString(string fileName)
{
string s = "";
StreamReader sr = new StreamReader(fileName);
string hang;
while ((hang = sr.ReadLine()) != null)
{
s += hang;
}
Console.WriteLine("打印json文件读出来的字符串------------------------------------");
Console.WriteLine(s);
return s;
}
}
/// <summary>
/// WeiJson类使用示例
/// </summary>
public static class WeiJsonUse
{
public static void WeiJsonFuncUse()
{
string FileName = @" C:\Users\Administrator\Desktop\测试表.csv";
string JsonFileName = "";
WeiJson wei = new WeiJson();
//读取csv文件生成json字符串
string strJSON = wei.WeiCsv2JSON(FileName);
strJSON = wei.WeiCsv2JsonMessageWeiJson(strJSON);
//保存文件,生成一个.json结尾的文件
JsonFileName = FileName.Substring(0, FileName.Length - 4) + ".json";
StreamWriter sw = new StreamWriter(JsonFileName);
sw.WriteLine(strJSON);
sw.Flush();
sw.Close();
//读取json文件 生成 json类对象
string str = wei.ReadFileToString(JsonFileName);
Console.WriteLine("打印将要转换成json对象的字符串");
Console.WriteLine(str);
WeiJsonStruct WeiJsonObject = wei.JsonStr2Struct(str);
//由json对象生成json文件
wei.WeiStruct2JsonStr(WeiJsonObject);
//由json对象生成csv文件
wei.WeiStruct2csvStr(WeiJsonObject);
}
}
//行结构体
[System.Serializable]
public class WeiHangType
{
public string name { get; set; } //需要和第一行保持一致
public string age { get; set; }
public string work01 { get; set; }
public string work02 { get; set; }
public string work03 { get; set; }
public string work04 { get; set; }
public string work05 { get; set; }
public string work06 { get; set; }
}
//JSON数据结构
[System.Serializable]
public class WeiJsonStruct
{
public string message;
public List<WeiHangType> WeiJson;
}
}
大伟C# JSON类
最新推荐文章于 2022-04-03 17:26:01 发布