将任何嵌套的Json String转换为csv

时间:2017-03-10 13:28:16

标签: c# json csv

我正在构建一个API,用于将任何嵌套的Json字符串转换为c#代码中的CSV。我试过javascriptserializer并试图操纵我的格式,但我失败了。我不能使用Class来反序列化,因为我需要使用任何嵌套或简单的Json字符串。我正在使用这个Json字符串。

{
"ccId" : "3HZN35631490",
"hId" : "21640500",
"identifiers" : [{  
"identityText" : "1234", 
"identityTypeCode" : "SSN"    
}],
"members" : [{  
"dob" : "2016-12-03",  
"firstName" : "HUG HES",  
"gender" : "M",  
"identifiers" : [ {
"identityText" : "1234",
"identityTypeCode" : "HNJI" 
},  
{
"identityText" : "467",
"identityTypeCode" : "HIE_EXSID"  
} ],  
"lastName" : "ABREUXSIT",  
"memberId" : "2179410",  
"optOutIndicator" : "N", 
  "relationship" : "SUBSCRIER" 
},
{   
"dob" : "2016-12-03",   
"firstName" : "DUG",   
"gender" : "M",  
"identifiers" : [  { 
"identityText" : "1234",
"identityTypeCode" : "HNJID" 
},  
{ 
"identityText" : "4567",
"identityTypeCode" : "HIE_EXSID"
}   ], 
  "lastName" : "ABREUXSIT",   
  "memberId" : "21794420",  
 "optOutIndicator" : "N",  
"relationship" : "CHILD" 
},
{
  "dob" : "2016-12-03",
"firstName" : "HUGE",  
"gender" : "M",  
"identifiers" : [  { 
"identityText" : "1234",  
"identityTypeCode" : "HNJID" 
}, 
{
"identityText" : "4567",
"identityTypeCode" : "HIE_EXSID" 
}  ], 
"lastName" : "ABREUXSIT",   
"memberId" : "21794420",  
"optOutIndicator" :"N",  
"relationship" : "CHILD"
}],
"subscriberFirstame" : "HUG HES",
"subscriberLastName" : "ABREUXSIT" 
}

我需要结果: -

ccid, hid, identifiers_identityText, identifiers_identityTypeCode, members_dob, members_firstName, members_gender, members_identifiers_identityText, members_identityTypeCode, members_lastname, members_memberid, member_OPtOutIndicator,member_relationship, SubscriberFirstName, SubscriberLastName 3HZN35631490, 21640500,1234, SSN, 2016-12-03, HUG HES, M, 1234, HNJI, ABREUXSIT,2179410,  N, SUBSCRIER,HUG HES, ABREUXSIT

3HZN35631490,21640500, , ,2016-12-03, HUG HES, M,467, HIE_EXSID, ABREUXSIT,2179410,  N, SUBSCRIER, HUG HES, ABREUXSIT

3HZN35631490,21640500, , ,2016-12-03, DUG, M, 1234, HNJID,  ABREUXSIT,21794420, N , CHILD, HUG HES, ABREUXSIT


3HZN35631490,21640500, , ,2016-12-03, DUG, M, 4567, HIE_EXSID,  ABREUXSIT,21794420, N , CHILD, HUG HES, ABREUXSIT


3HZN35631490,21640500, , ,2016-12-03, HUGE, M, 1234, HNJID,  ABREUXSIT,21794420, N , CHILD, HUG HES, ABREUXSIT


3HZN35631490,21640500, , ,2016-12-03, HUGE, M, 4567, HIE_EXSID,  ABREUXSIT,21794420, N , CHILD, HUG HES, ABREUXSIT

1 个答案:

答案 0 :(得分:0)

Json是层次结构数据,您需要展平才能从中创建CSV文件。您的JSON很复杂,其中包含2个嵌套列表子项。使用Cinchoo ETL(一个开源框架),您可以简化json数据并快速生成CSV文件。

下面的代码显示了如何操作

using (var jr = new ChoJSONReader("sample4.json").Configure(c => c.UseJSONSerialization = true))
{
    using (var xw = new ChoCSVWriter("sample4.csv").WithFirstLineHeader())
    {
        foreach (JObject jItem in jr)
        {
            dynamic item = jItem;
            var identifiers = ChoEnumerable.AsEnumerable(jItem).Select(e => ((IList<JToken>)((dynamic)e).identifiers).Select(i =>
                new
                {
                    identityText = i["identityText"].ToString(),
                    identityTypeCode = i["identityTypeCode"].ToString()
                })).SelectMany(x => x);

            var members = ChoEnumerable.AsEnumerable(jItem).Select(e => ((IList<JToken>)((dynamic)e).members).Select(m => ((IList<JToken>)((dynamic)m).identifiers).Select(i =>
                new
                {
                    dob = m["dob"].ToString(),
                    firstName = m["firstName"].ToString(),
                    gender = m["gender"].ToString(),
                    identityText = i["identityText"].ToString(),
                    identityTypeCode = i["identityTypeCode"].ToString(),
                    lastname = m["lastName"].ToString(),
                    memberId = m["memberId"].ToString(),
                    optOutIndicator = m["optOutIndicator"].ToString(),
                    relationship = m["relationship"].ToString()

                }))).SelectMany(x => x).SelectMany(y => y);

            var comb = members.ZipEx(identifiers, (m, i) =>
            {
                if (i == null)
                    return new
                    {
                        item.ccId,
                        item.hId,
                        identifiers_identityText = String.Empty,
                        identifiers_identityTypeCode = String.Empty,
                        members_dob = m.dob,
                        members_firstName = m.firstName,
                        members_gender = m.gender,
                        members_identifiers_identityText = m.identityText,
                        members_identityTypeCode = m.identityTypeCode,
                        members_lastname = m.lastname,
                        members_memberid = m.memberId,
                        member_optOutIndicator = m.optOutIndicator,
                        member_relationship = m.relationship,
                        SubscriberFirstName = item.subscriberFirstame,
                        SubscriberLastName = item.subscriberLastName,

                    };
                else
                    return new
                    {
                        item.ccId,
                        item.hId,
                        identifiers_identityText = i.identityText,
                        identifiers_identityTypeCode = i.identityTypeCode,
                        members_dob = m.dob,
                        members_firstName = m.firstName,
                        members_gender = m.gender,
                        members_identifiers_identityText = m.identityText,
                        members_identityTypeCode = m.identityTypeCode,
                        members_lastname = m.lastname,
                        members_memberid = m.memberId,
                        member_optOutIndicator = m.optOutIndicator,
                        member_relationship = m.relationship,
                        SubscriberFirstName = item.subscriberFirstame,
                        SubscriberLastName = item.subscriberLastName,
                    };

            });
            xw.Write(comb);
        }
    }
}

CSV输出

ccId,hId,identifiers_identityText,identifiers_identityTypeCode,members_dob,members_firstName,members_gender,members_identifiers_identityText,members_identityTypeCode,members_lastname,members_memberid,member_optOutIndicator,member_relationship,SubscriberFirstName,SubscriberLastName
3HZN35631490,21640500,1234,SSN,2016-12-03,HUG HES,M,1234,HNJI,ABREUXSIT,2179410,N,SUBSCRIER,HUG HES,ABREUXSIT
3HZN35631490,21640500,,,2016-12-03,HUG HES,M,467,HIE_EXSID,ABREUXSIT,2179410,N,SUBSCRIER,HUG HES,ABREUXSIT
3HZN35631490,21640500,,,2016-12-03,DUG,M,1234,HNJID,ABREUXSIT,21794420,N,CHILD,HUG HES,ABREUXSIT
3HZN35631490,21640500,,,2016-12-03,DUG,M,4567,HIE_EXSID,ABREUXSIT,21794420,N,CHILD,HUG HES,ABREUXSIT
3HZN35631490,21640500,,,2016-12-03,HUGE,M,1234,HNJID,ABREUXSIT,21794420,N,CHILD,HUG HES,ABREUXSIT
3HZN35631490,21640500,,,2016-12-03,HUGE,M,4567,HIE_EXSID,ABREUXSIT,21794420,N,CHILD,HUG HES,ABREUXSIT

希望这有帮助。

披露:我是这个图书馆的作者。