Last active
July 8, 2020 09:10
-
-
Save RutgerAlbers/aede12416d5d8cba11a0a7687d11584e to your computer and use it in GitHub Desktop.
Remove dynamic root from JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dynamicRootName": [ | |
{ | |
"id": "testId1", | |
"message": "testMessage1" | |
}, | |
{ | |
"id": "testId2", | |
"message": "testMessage2" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "testId1", | |
"message": "testMessage1" | |
}, | |
{ | |
"id": "testId2", | |
"message": "testMessage2" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string RemoveJsonRoot(string jsonString) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonString))) | |
{ | |
JObject jObject = JObject.Load(reader); | |
var rootContent = jObject.First.Children<JToken>(); | |
foreach (var r in rootContent) | |
{ | |
sb.Append(r.ToString()); | |
} | |
return sb.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment