0

I am using a library handlebars.net. https://github.com/rexm/Handlebars.Net

Which takes a template string and an anonymous type and make the template filled with the anonymous type values. Here is an example:

string source =
@"<div class=""entry"">
  <h1>{{title}}</h1>
  <div class=""body"">
    {{body}}
  </div>
</div>";

var template = Handlebars.Compile(source);

var data = new {
    title = "My new post",
    body = "This is my first post!"
};

var result = template(data);

/* Would render:
<div class="entry">
  <h1>My New Post</h1>
  <div class="body">
    This is my first post!
  </div>
</div>
*/

In my case, I have a json file that I want to read from and use that as the anonymous type. If I use a json parser like newtonsoft, I get back a JSONObject type variable, and it works for basic values, but if I use arrays, it throws an exaction about not being able to convert a JArray to a String.

So my question is, is there a way to convert a json file into an anonymous type?

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types

Thanks

3
  • 1
    If you want to show your C# code using Json.NET, we could probably help with that. Otherwise, this looks a lot like Deserialize JSON into C# dynamic object? Commented Feb 22, 2019 at 17:45
  • What you are talking about is "Deserializing JSON objects" -- there is plenty of reference materials on the web. including the article referenced by @HereticMonkey Commented Feb 22, 2019 at 19:07
  • Possible duplicate of Deserialize JSON into C# dynamic object? Commented Feb 22, 2019 at 21:02

2 Answers 2

2

Consider using ServiceStack's dynamic APIs to deserialize arbitrary JSON.

https://github.com/ServiceStack/ServiceStack.Text#supports-dynamic-json

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

1 Comment

I solved it with this Dictionary<string, Object> result = (Dictionary<string, Object>)JSON.parse(json);
0

Well it would help if you provided an actual code sample that fails on an array, but Newtonsoft JSON has no problem parsing any valid JSON, so I beleive problem must be with your code

fiddle: https://dotnetfiddle.net/e0q5mO

var s = "{\"a\":[1,2,3]}";
dynamic json = JsonConvert.DeserializeObject(s);
var a = json.a.ToObject<int[]>();
Console.WriteLine(a[0]);

This is just one way to do it.

Problem with deserializing to anonymous type is that they are anonymous. Thus you have no way of creating it's instance other than with new { a, b = c } expression. So if you have to deserialize to a strictly typed instance you have to describe it. Like this:

public class MyDto
{
    public int [] a;
}

then you will be able to just deserialize it with var json = JsonConvert.DeserializeObject<MyDto>(s);

Comments

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.