c# 将json转换成Dictionary字符串

          方法一

               Dictionary<string, string> DicContent = postdata.Trim().Split(',').ToDictionary(s => s.Split(':')[0].Replace("\"", ""), s => s.Split(':')[1].Replace("\"", "")); 

      方法二

             JavaScriptSerializer s = new JavaScriptSerializer();


  string jsonTexts = "{\"count\":\"5550\",\"status\": \"200\",\"message\": \"success\",\"show_data\":[{\"productsId\":\"10025\",\"productName\":\"粉色波点裙蕾丝帽子熊 00190009\",\"discountPrice\":\"98.00\",\"defaultPrice\":\"null\",\"frontImg_160_160\":\"http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026_160_160.jpg\",\"frontImg\":\"http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026.jpg\",\"totalSales\":\"1\",\"shopName\":\"喜洋洋精品儿童玩具专卖店\"},{\"productsId\":\"10026\",\"productName\":\"粉色波点裙蕾丝帽子熊 00190009\",\"discountPrice\":\"98.00\",\"defaultPrice\":\"null\",\"frontImg_160_160\":\"http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026_160_160.jpg\",\"frontImg\":\"http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026.jpg\",\"totalSales\":\"1\",\"shopName\":\"喜洋洋精品儿童玩具专卖店\"}]}";

 

Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(jsonTexts);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 C# 中的 Newtonsoft.Json 库来将 JSON 换为 CSV 文件。下面是一个简单的示例代码: ```csharp using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; class Program { static void Main(string[] args) { // 读取 JSON 文件内容 string json = File.ReadAllText("input.json"); // 将 JSON 解析为对象 var data = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(json); // 获取所有属性名作为 CSV 的表头 var headers = new List<string>(data[0].Keys); // 创建 CSV 文件并写入表头 using (var writer = new StreamWriter("output.csv")) { writer.WriteLine(string.Join(",", headers)); // 逐行写入数据 foreach (var item in data) { var values = new List<string>(); // 按照表头的顺序获取每个属性的值 foreach (var header in headers) { values.Add(item.ContainsKey(header) ? item[header] : ""); } writer.WriteLine(string.Join(",", values)); } } Console.WriteLine("CSV 文件已生成!"); } } ``` 在这个示例中,我们假设输入的 JSON 数据是一个包含多个对象的数组,每个对象都是一个字典。通过使用 Newtonsoft.Json 库,我们可以将 JSON 字符串反序列化为一个包含字典的列表。然后,我们提取出所有属性名作为 CSV 文件的表头,并逐行写入每个对象的属性值。 记得替换代码中的 `input.json` 和 `output.csv` 分别为你的输入 JSON 文件和输出 CSV 文件的路径。确保你已经将 Newtonsoft.Json 包添加到你的项目中。 希望对你有所帮助!如果有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值