简体   繁体   中英

How to get property from dynamic JObject programmatically

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 eg:

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.

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!

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM