使用foreach循环将单独的值放入字典中的行

时间:2018-12-15 02:38:16

标签: c# json.net

我正在尝试从JSON文件中获取解析的值,并将其转换为行。我已经整周尝试了,但仍然无法解决。


我当前的输出如下:

a: 1
b: 2
c: 3
a: 1a
b: 2a
c: 3a
a: 1b
b: 2b
c: 3b

我希望输出是这样,但找不到解决方案。

a   b   c 
1   2   3
1a  2a  3a
1b  2g  3b

这是关键:我需要能够使用类似于以下格式的行写入行:


   foreach (var item in MyStuff)
        {

        Console.WriteLine("{0}, {1},{2}",
        item.a, item.b, item.c
         );

我该怎么做?这有可能吗?我已经尝试了好几天了。


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

public class Program
{
    public static void Main()
    {
        string json = 
        @"{
            'somethingone': 'abc',
            'somethingtwo': 'abcde-1234',
            'information': 
            {
                'report': [
                     {
                        'a': '1',
                        'b': '2',
                        'c': '3'
                     },
                     {
                        'a': '1a',
                        'b': '2a',
                        'c': '3a'
                     },
                     {
                        'a': '1b',
                        'b': '2b',
                        'c': '3b'
                     },
                 ]
             }
        }";

        RootObj mainObj = JsonConvert.DeserializeObject<RootObj>(json);

        Console.WriteLine("somethingone: " + mainObj.somethingone);
        Console.WriteLine("somethingtwo: " + mainObj.somethingtwo);

        foreach (Dictionary<string, string> report in mainObj.information.report)
        {
            foreach (KeyValuePair<string, string> item in report)
            {
                string key = item.Key;
                string value = item.Value;

                Console.WriteLine(key + ": " + value);
            }
        }
    }
}

class RootObj
{
    public string somethingone { get; set; }
    public string somethingtwo { get; set; }
    public Information information { get; set; }
}

class Information
{
    public Dictionary<string, string>[] report { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您已经掌握了大部分方法,只需要弄清楚如何旋转结构即可。

这应该让您入门。

// define a format
var columnFormat = "{0,-4}";

// get the column names
var keys = mainObj.information.report.First().Keys.ToList();

// print the column headers
keys.ForEach(key => Console.Write(columnFormat, key));
Console.WriteLine();

// print the rows
foreach (var report in mainObj.information.report)
{
    keys.ForEach(key => Console.Write(columnFormat, report[key]));
    Console.WriteLine();
}

这可以工作,但是很脆弱。如果您知道json的报表部分将始终包含结构完全相同的条目(具有相同名称的相同列数),那么它将起作用。

但是,如果我们这样弄乱您报告中的列,该怎么办?

'report': [
        {
            'a': '1',
            'c': '3'
        },
        {
            'a': '1a',
            'b': '2a',
            'c': '3a',
            'd': '4a'
        },
        {
            'a': '1b',
            'b': '2b',
            'c': '3b'
        },

现在上述解决方案将失效,因此在获取列名称和值时您需要更加灵活。

// get the column names
var keys = mainObj.information.report
              .SelectMany(x => x.Keys)
              .Distinct()
              .OrderBy(key => key)
              .ToList();

// print the column headers
keys.ForEach(key => Console.Write(columnFormat, key));
Console.WriteLine();

// print the rows
foreach (var report in mainObj.information.report)
{
    keys.ForEach(key =>
    {
        string value;
        report.TryGetValue(key, out value);

        Console.Write(columnFormat, value);
    });

    Console.WriteLine();
}

这应该导致输出如下:

a   b   c   d
1       3
1a  2a  3a  4a
1b  2b  3b