Trying to get this to work in a console app for a simple example. I'm using Json.Net and DalSoft.RestClient packages from nuget. I'm not married to either one of these I'm just trying to get the job done. Open to an alternative.
http://jsonplaceholder.typicode.com/users returns valid JSON.
I want to enumerate the properties that are unknown in advance. The following seemed viable but fails with a System.AggregateException - RuntimeBinderException: Cannot perform runtime binding on a null reference I feel like I have missed something simple.
using System;
using System.Threading.Tasks;
using DalSoft.RestClient;
using Newtonsoft.Json.Linq;
namespace ImTired
{
class Program
{
static void Main(string[] args)
{
DoThingsAsync().Wait();
Console.ReadLine();
}
private static async Task<object> DoThingsAsync()
{
dynamic client = new RestClient("http://jsonplaceholder.typicode.com");
var user = await client.Users.Get();
var foo = user[0].name; // grab the first item (Works to this point)
//JProperty item = new JProperty(user);
var item = user[0];
foreach (JProperty property in item.Properties()) //(fails here I've missed a cast or conversion)
{
Console.WriteLine(property.Name + " - " + property.Value);
}
Console.WriteLine("Call Complete: " + foo);
return null;
}
}
}
Follow up EDIT: Added this for any lurkers. I needed to add the assemblies to the GAC to make it available for a scripting tool. This for your Strong Name information.
I understand that there is probably another, better, way to reference a non GAC'd assembly. I just don't have time to figure it out. Maybe I'll form a meaningful question here.
DalSoft Rest Client does not expose JsObject/JsArray.It wraps JSON inside RestClientResponseObject and exposes as a string with ToString method.To get all JSON properties, we need to parse explicitly string to JObject/JArray.
var item =JObject.Parse(user[0].ToString());
OR
var users = JArray.Parse(user.ToString());
var item = users[0];