13

I have a json string with the following structure

{
    "resource": "user",
    "method": "create",
    "fields": {
        "name": "John",
        "surname: "Smith",
        "email": "[email protected]"
    }
}

The keys inside fields are variable, that means I don't know them in advance

So, instead of deserializing a json string to an object, I need to traverse the json, in order to get the properties inside fields in a Dictionary or something like that.

I heard about the Json.NET library and it's ability to parse dynamic jsons, but I'm not sure it it's already included in net-core or not.

What would be the standard / easiest way to accomplish that in net-core 2.0. Code example would be appreciated.

1
  • 1
    Yes, you can use Json.NET in .NET Core. Commented Sep 4, 2017 at 3:50

4 Answers 4

26

Yes. You can add Newtonsoft.json package to your .net core project. And to query the dynamic json object, you can use the JObject object provided by the library to parse your json into a dynamic object. Here is the link for the document.

Given your json sample it may look like this

 var resource = JObject.Parse(json);
 foreach (var property in resource.fields.Properties())
 {
   Console.WriteLine("{0} - {1}", property.Name, property.Value);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me: foreach (var property in resource.Properties())
6

Json.NET is the go-to library when you are serializing .NET objects. However, when structure of objects is not static, APIs from System.Json namespace will be simpler to use. System.Json can be used in .NET Core 2.0 by installing a package from NuGet like this:

dotnet add package System.Json --version 4.4.0

Here is a nice tutorial on how to use APIs from System.Json namespace: Working with JSON in .NET – a Silverlight example

Comments

0

In my case, I needed a serialization + deserialization ability which is supported out of the box (no 3rd party libs). It seem logical to have these features implemented, taking in considerations that JSON is pretty much everywhere and that .net core project is mature

I followed this great article that explains about System.Text.Json new implementation

In short: you can choose to model the desirable data using a class or use a general implementation in order to parse JSON documents in a more generic way

Both options are covered beautifully in the article.. just adding a short code snippet that answers the original question

var json = @"{""hello"":""world""}";

var options = new JsonDocumentOptions
{
    AllowTrailingCommas = true
};

var doc = JsonDocument.Parse(json, options);

Comments

-2
using System.Web.Script.Serialization;
using System.Dynamic;

var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic jsonObject = serializer.Deserialize(jsonString, typeof(Example));

1 Comment

what package is this using? System.Json?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.