c# - how to get Json nested properties to primary one -
i have below scenario:
this class structure :
public class user { public string firstname { get; set; } public string lastname { get; set; } public system.collections.objectmodel.collection<likes> likes { get; set; } } public class likes { public string sport { get; set; } public string music { get; set; } public string food { get; set; } public string place { get; set; } }
when serialize object of user class generate below json string :
{"firstname":"naresh", "lastname":"parmar", "likes": [{"sport":"cricket", "music":"classic", "food":"gujarati", "place":"india"}] }
i want generate above json string below:
{"firstname":"naresh", "lastname":"parmar", "sport":"cricket", "music":"classic", "food":"gujarati", "place":"india" }
i want nested properties primary one.
any appreciated.
thanks in advance..
edit:
{"firstname":"naresh", "lastname":"parmar", "sport":"cricket,chess,football", "music":"classic", "food":"gujarati", "place":"india" }
it's bad practice, since code i'll post bellow doesn't have great maintainability, if that's looking for, can use this. class have format you'd like, , have method adds list of likes format you've required. class should serialize json:
class nesteduser { public string firstname { get; set; } public string lastname { get; set; } public string sport { get; set; } public string music { get; set; } public string food { get; set; } public string place { get; set; } public void addlikes(system.collections.objectmodel.collection<likes> likes) { foreach (likes in likes) { sport += like.sport + ","; music += like.music + ","; food += like.food + ","; place += like.place + ","; } if (sport != string.empty) { sport = sport.substring(0, sport.length - 1); } if (music != string.empty) { music = music.substring(0, music.length - 1); } if (food != string.empty) { food = food.substring(0, food.length - 1); } if (place != string.empty) { place = place.substring(0, place.length - 1); } } }
Comments
Post a Comment