34

I'm parsing a JSON string using the NewtonSoft JObject. How can I get values from a dynamic object programmatically? I want to simplify the code to not repeat myself for every object.

public ExampleObject GetExampleObject(string jsonString)
{
ExampleObject returnObject = new ExampleObject();
dynamic dynamicResult = JObject.Parse(jsonString);
if (!ReferenceEquals(dynamicResult.album, null))
   {
       //code block to extract to another method if possible
       returnObject.Id = dynamicResult.album.id; 
       returnObject.Name = dynamicResult.album.name;
       returnObject.Description = dynamicResult.albumsdescription;
       //etc..
   }
else if(!ReferenceEquals(dynamicResult.photo, null))
   {
       //duplicated here
       returnObject.Id = dynamicResult.photo.id;
       returnObject.Name = dynamicResult.photo.name;
       returnObject.Description = dynamicResult.photo.description;
       //etc..
   }
else if..
//etc..

return returnObject;
}

Is there any way I can extract the code blocks in the "if" statements to a separate method e.g:

private void ExampleObject GetExampleObject([string of desired type goes here? album/photo/etc])
{
  ExampleObject returnObject = new ExampleObject();
  returnObject.Id = dynamicResult.[something goes here?].id;
  returnObject.Name = dynamicResult.[something goes here?].name;
  //etc..
  return returnObject;
}

Is it even possible since we can't use reflection for dynamic objects. Or am I even using the JObject correctly?

Thanks.

3
  • 1
    Is the jsonString a string that is under your control? Or are you receiving this from another party and you need to communicate with it? Commented Apr 19, 2013 at 5:59
  • @MichaelD it's from another party. I'm just receiving and parsing. Commented Apr 19, 2013 at 13:05
  • More answers see deserializing JSON to .net object using NewtonSoft (or linq to json maybe?) Commented Jul 22, 2016 at 7:55

3 Answers 3

59

Assuming you're using the Newtonsoft.Json.Linq.JObject, you don't need to use dynamic. The JObject class can take a string indexer, just like a dictionary:

JObject myResult = GetMyResult();
returnObject.Id = myResult["string here"]["id"];

Hope this helps!

Sign up to request clarification or add additional context in comments.

5 Comments

What goes in ["string here"]? Wouldn't "id" just return the value of ID in the object?
Is this case insensitive?
@joelforsyth, yes you might do, depending on the object structure, e.g. returnObject.Id = (int)myResult["id"];
@mardok No, unfortunately it is case sensitive and so it may or may not work depending on the JsonSerializer settings that were used to serialize the object.
Wow! Thanks for the tip! I was trying to use GetProperty. For me I needed to apply it with a cast similar to this (but different type): String MyProperty1Value = (String)((Newtonsoft.Json.Linq.JObject)This.Tag)["MyProperty1"]; This just for an example, not my actual code. I was using a lambda fct.
7

Another way of targeting this is by using SelectToken (Assuming that you're using Newtonsoft.Json):

JObject json = GetResponse();
var name = json.SelectToken("items[0].name");

For a full documentation: https://www.newtonsoft.com/json/help/html/SelectToken.htm

Comments

0

with dynamic keyword like below:

private static JsonSerializerSettings jsonSettings;

private static T Deserialize<T>(string jsonData)
{
   return JsonConvert.DeserializeObject<T>(jsonData, jsonSettings);
}

//if you know what will return

var jresponse = Deserialize<SearchedData>(testJsonString);

//if you know return object type you should sign it with json attributes like

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class SearchedData
{
    [JsonProperty(PropertyName = "Currency")]
    public string Currency { get; set; }
    [JsonProperty(PropertyName = "Routes")]
    public List<List<Route>> Routes { get; set; }
}

// if you don't know the return type use dynamic as generic type

var jresponse = Deserialize<dynamic>(testJsonString);

2 Comments

what dynamic keyword, you did not even write dynamic in your response?
joedotnot i have corrected my answer use like that, if you face any problem please write again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.