I need help in converting following code which is using newtonsoft to System.Text.Json
string myList = null!;
string jSon = /* From some source */;
object jsonObject = JsonConvert.DeserializeObject(jSon, typeof(object));
// Loop through the keys
foreach (var jsonList in ((Newtonsoft.Json.Linq.JObject)(jsonObject)))
{
if (jsonList.Key == "myKey")
{
foreach (object o in (jsonList.Value.ToArray()))
{
myList = o.ToString().ToUpper() ',';
}
}
}
json like:
{"myKey":["fir","dsdsd"],"status":"ok"}
CodePudding user response:
With System.Text.Json, you can parse arbitrary JSON using either the JsonDocument
or JsonNode
document object models. The differences are:
JsonNode
is modifiable and is, in my opinion, easier to work with as it most closely resembles Newtonsoft's LINQ to JSON model. It was added in .NET 6.JsonDocument
is read-only, but may be a little more memory-efficient in parsing huge JSON files. It was introduced as part of the original System.Text.Json API in .NET Core 3.0.
Thus, using JsonNode
, your code can be rewritten as follows:
static string QueryJson(string json, string myKey, bool addComma = false)
{
// Parse to a JsonNode and cast to JsonObject. An exception is thrown if not an object
var node = JsonNode.Parse(json)!.AsObject();
// Query the array of strings
var query = (node[myKey] as JsonArray)?.Select(i => i?.ToString().ToUpper()) ?? Enumerable.Empty<string>();
if (addComma)
query = query.Concat(new [] { "" });
// Join the string array into a single comma-separated string
var myString = string.Join(',', query);
return myString;
}
And here is a version of the same logic using JsonDocument
:
static string QueryJsonWithJsonDocument(string json, string myKey, bool addComma = false)
{
// Parse to a JsonNode and cast to JsonObject. An exception is thrown if not an object
using var doc = JsonDocument.Parse(json);
// Query the array of strings
var query = doc.RootElement.GetProperty(myKey).EnumerateArray().Select(e => e.GetString()?.ToUpper());
if (addComma)
query = query.Concat(new [] { "" });
// Join the string array into a single comma-separated string
var myString = string.Join(',', query);
return myString;
}
Notes:
You tagged your question .net-core-3.1. This version goes out of support in about a month, on December 13, 2022. If you are using this version you will need to use
JsonDocument
sinceJsonNode
was introduced in .NET 6.JsonDocument
is disposable, and should be disposed to ensure that pooled memory is returned to the system.JsonNode
is not disposable.It will be more efficient to use
string.Join()
than manually building a comma-separated string using multiple string additions.Your current code adds a trailing comma to the string:
FIR,DSDSD,
. This looks like a bug, but if you want that you can makestring.Join()
add a trailing comma by adding an extra empty string to the query.
Demo fiddle here.