问题
I am trying to take the values I have parsed from a JSON file and convert them into rows. I have tried all week, but still cannot figure this out.
My current output looks like this:
a: 1
b: 2
c: 3
a: 1a
b: 2a
c: 3a
a: 1b
b: 2b
c: 3b
I want my output to be like this, but I cannot find a solution.
a b c
1 2 3
1a 2a 3a
1b 2g 3b
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:
You're writing out the key-value pairs for each dictionary one pair per line.
What you want to do instead is first write out a line containing the keys from the first dictionary, then write out one line for each dictionary containing its values.
You can use Write
instead of WriteLine
to write each value without a line break. Then at the end of the line, use WriteLine
to add the line break.
Here is what the code looks like:
Dictionary<string, string> firstDict = mainObj.information.report.FirstOrDefault();
if (firstDict != null)
{
string format = "{0,-4} "; // change the negative number to change the column width
// write out the headers by getting the keys from the first dictionary
foreach (string key in firstDict.Keys)
{
Console.Write(string.Format(format, key));
}
Console.WriteLine();
// now write out the values for each dictionary
foreach (Dictionary<string, string> dict in mainObj.information.report)
{
foreach (string value in dict.Values)
{
Console.Write(string.Format(format, value));
}
Console.WriteLine();
}
}
Note: the code above assumes that each dictionary in the report will have the same set of keys. If this is not true, then you will need to make some adjustments to the code to handle the irregularities.
Fiddle: https://dotnetfiddle.net/9T9vBV
回答2:
You can convert your json to table format with only one line of code than can convert your parsed json to DataTable
directly like
DataTable dt = jToken["information"]["report"].ToObject<DataTable>();
So you can take advantage of JToken
and DataTable
together to deserialize your json to directly into table like
JToken jToken = JToken.Parse(json);
DataTable dt = jToken["information"]["report"].ToObject<DataTable>();
Console.WriteLine("a \t b \t c");
Console.WriteLine("-------------------");
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine($"{dr["a"]} \t {dr["b"]} \t {dr["c"]}");
}
Console.ReadLine();
Output:
Live Demo
来源:https://stackoverflow.com/questions/53788239/taking-json-output-into-table-format