[英]How can I Iterate through dynamic json c#?
I'm getting a response from an api, but this response could be any type of json. 我从api得到响应,但是此响应可以是任何类型的json。 I need to take the data and print it on a csv, is there anyway to convert that json to a dynamic object and iterate it or is there any other way to do it?. 我需要获取数据并将其打印在csv上,是否有将json转换为动态对象并进行迭代的方法,还是有其他方法可以实现?
My json response is: 我的json响应是:
{
"A":{
"B":[
{
"C":"data"
"E":"data"
"G":{
"H":[
{"I":"data"},
{...},
{...}
]
}
},
{...},
{...},
{...}
]
}
} }
and latter it could be 后者可能是
{
"A":{
"B":[
{
"C":"data"
"E":"data"
"G":{
"H":[
{"I":"data"},
{...},
{...},
{"K":[
{"w":"data"},
{...},
{...},
{"d":[
{"d":"data"},
{...},
{"d":[
{"h","data"},
{...},
{...}
]}
]}
]}
]
}
},
{...},
{...},
{...}
]
}
} }
With Cinchoo ETL - an open source library available to parse json and convert them to CSV 使用Cinchoo ETL-可用于解析json并将其转换为CSV的开源库
string json = @"
{
""name"":""John"",
""age"":30,
""cars"": {
""car1"":""Ford"",
""car2"":""BMW"",
""car3"":""Fiat"",
""Country"": [
""USA"",
""Mexico""
]
}
}";
StringBuilder csv = new StringBuilder();
using (var p = ChoJSONReader.LoadText(json)
.WithJSONPath("$")
)
{
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
)
{
w.Write(p);
}
}
Console.WriteLine(csv.ToString());
Output: 输出:
name,age,cars_car1,cars_car2,cars_car3,cars_Country_0,cars_Country_1
John,30,Ford,BMW,Fiat,USA,Mexico
Disclaimer: I'm the author of this library. 免责声明:我是这个图书馆的作者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.