将嵌套的JSON转换为CSV [英] Convert nested JSON to CSV
问题描述
我正在将具有10个以上级别的嵌套JSON对象转换为C#.NET中的CSV文件.
I am converting a nested JSON object with more than 10 levels to CSV file in C# .NET.
我一直在使用JavaScriptSerializer().Deserialize<ObjectA>(json)
或XmlNode xml = (XmlDocument)JsonConvert.DeserializeXmlNode(json)
分解对象.有了这些对象,我可以进一步将其写入CSV文件.但是,现在JSON对象进一步扩展了.大多数未真正使用的数据,所以我更喜欢原始数据转储.
I have been using JavaScriptSerializer().Deserialize<ObjectA>(json)
or XmlNode xml = (XmlDocument)JsonConvert.DeserializeXmlNode(json)
to break down the object. With the objects I can further write into CSV file. However now the JSON object further expand. Most data that is not really in use so I would prefer a raw data dump.
是否可以将数据转储为csv格式而不用声明结构呢?
Is that easier way I can just dump the data into csv format without declaring the structure?
示例JSON
{
"F1":1,
"F2":2,
"F3":[
{
"E1":3,
"E2":4
},
{
"E1":5,
"E2":6
},
{
"E1":7,
"E2":8,
"E3":[
{
"D1":9,
"D2":10
}
]
},
]
}
我期望的CSV输出是
F1,F2,E1,E2,D1,D2
1,2
1,2,3,4
1,2,5,6
1,2,7,8,9,10
推荐答案
您的请求中有一个不一致的地方:您希望为具有子对象的根对象生成一行,但是您不希望将其作为一行为"F3[2]"
对象生成的,该对象也具有子对象.听起来您的规则是,为具有至少一个原始值属性的对象打印一行,只要该对象是根对象或不具有至少具有一个原始值属性的后代对象" .这有点棘手,但是可以使用 LINQ to JSON >
There's an inconsistency in your request: you want a row to be generated for the root object, which has children, but you don't want a row to be generated for the "F3[2]"
object, which also has children. So it sounds like your rule is, "print a row for an object with at least one primitive-value property, as long as that object is either the root object or does not have descendant objects with at a least one primitive-value property". That's a little tricky, but can be done with LINQ to JSON
var obj = JObject.Parse(json);
// Collect column titles: all property names whose values are of type JValue, distinct, in order of encountering them.
var values = obj.DescendantsAndSelf()
.OfType<JProperty>()
.Where(p => p.Value is JValue)
.GroupBy(p => p.Name)
.ToList();
var columns = values.Select(g => g.Key).ToArray();
// Filter JObjects that have child objects that have values.
var parentsWithChildren = values.SelectMany(g => g).SelectMany(v => v.AncestorsAndSelf().OfType<JObject>().Skip(1)).ToHashSet();
// Collect all data rows: for every object, go through the column titles and get the value of that property in the closest ancestor or self that has a value of that name.
var rows = obj
.DescendantsAndSelf()
.OfType<JObject>()
.Where(o => o.PropertyValues().OfType<JValue>().Any())
.Where(o => o == obj || !parentsWithChildren.Contains(o)) // Show a row for the root object + objects that have no children.
.Select(o => columns.Select(c => o.AncestorsAndSelf()
.OfType<JObject>()
.Select(parent => parent[c])
.Where(v => v is JValue)
.Select(v => (string)v)
.FirstOrDefault())
.Reverse() // Trim trailing nulls
.SkipWhile(s => s == null)
.Reverse());
// Convert to CSV
var csvRows = new[] { columns }.Concat(rows).Select(r => string.Join(",", r));
var csv = string.Join("\n", csvRows);
Console.WriteLine(csv);
使用
public static class EnumerableExtensions
{
// http://stackoverflow.com/questions/3471899/how-to-convert-linq-results-to-hashset-or-hashedset
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
{
return new HashSet<T>(source);
}
}
哪个输出:
F1,F2,E1,E2,D1,D2
1,2
1,2,3,4
1,2,5,6
1,2,7,8,9,10
这篇关于将嵌套的JSON转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!