2017-03-10 106 views
0

我正在構建一個API以將任何嵌套的Json字符串轉換爲c#代碼中的CSV。我曾嘗試javascriptserializer,並試圖操縱我的格式,但我失敗了。我不能使用類來反序列化,因爲我需要使用任何嵌套或簡單的Json字符串。我正在使用這個Json字符串。將任何嵌套的Json字符串轉換爲csv

{ 
"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 

回答

0

JSON是層次結構的數據,你需要爲了從它創建CSV文件拉平。你的JSON很複雜,裏面有兩個嵌套列表子節點。通過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 

希望這有助於。

信息披露:我是該圖書館的作者。