Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

485
Views
Dynamic retrieve json element .NET 6

I want to retrieve a single value from a json string.

Previously I used Newtonsoft like this:

var jsonString = @"{ ""MyProp"" : 5 }";
dynamic obj = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
        
Console.WriteLine(obj["MyProp"].ToString());

But I can't seem to get it to work in .NET 6:

I've tried this so far:

var jsonString = @"{ ""MyProp"" : 5 }";
dynamic obj = await System.Text.Json.JsonSerializer.Deserialize<dynamic>(jsonString);
        
Console.WriteLine(obj.MyProp.ToString());

which results in this error:

Unhandled exception. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'System.Text.Json.JsonElement.this[int]' has some invalid arguments

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Reading upon this github, I had success using this approach:

NET 6 will include the JsonNode type which can be used to serialize and deserialize dynamic data.

Trying it results in:

using System;
                    
public class Program
{
    public static void Main()
    {
        var jsonString = @"{ ""MyProp"" : 5 }";
        //parse it
        var myObject = System.Text.Json.JsonDocument.Parse(jsonString);
        //retrieve the value
        var myProp= myObject.RootElement
                            .GetProperty("MyProp");
        
        Console.WriteLine(myProp);
    }
}

Which seems to work in my case.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!