Developer forum

Forum » Templates » Parsing JSON in Razor

Parsing JSON in Razor

Jacob Storgaard Jensen
Reply

Hi guys,

I'm new to all this Razor, and would appreciate any help on this problem i'm having:

I would like to retrieve the thumnail_url value from this JSON string output by this URL (Or similar): https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/76979871

I would get the ID number in the end of the URL from an item text field making af variable constructed like:
var videoURL = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/" + @videoID;

But I "just" need a way to stick my paws into the resulting JSON output, and retrieve the thumbnail_url.
Might it be an idea with some kind of caching?

Can't find my way around the different Json.net, JavaScriptSerializer and DataContractJsonSerializer articles out there :-(
Any ideas as to what would work on a standard Hostnordic hosted solution?


Replies

 
Sten Hougaard
Reply

Hi Jacob,

You may want to create a class which which you then can deserialize your JSON into. In that way you can access all the JSON data in C# through instances of that class.

It is very easy - On this page: http://json2csharp.com/ you simply paste the JSON in and voila you then get a Class generated for you to use in your Razor code.

I have not tried to deserialize using .NET but I have posted on the Dynamicweb Github Razor wiki an example of doing the oppersite which should be somewhat the same no matter what way you do the conversion :-) I guess out there in forum-land other more sharp can see and advice you how to do that.

https://github.com/dynamicweb/razor/wiki/Recipes#example-serialized-output

/Sten Hougaard
@netsi1964 - for hire, read CV - also freelance.

 
Jacob Storgaard Jensen
Reply

Hi Sten,

Thanks, but I'm so new to Razor and C# that I have no clue where to go from that :-D

 
Klavs Martens
Reply

Hi Jacob

Try this code. Remember to add the following using statements

using System.Net;

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

And add the json.net nuget package to refrences.

------------- CODE --------------

string url = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/76979871";

WebClient wc = new WebClient();

var json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString(url));

var thumbnail = json["thumbnail_url"].ToString();

Console.Write(thumbnail);

 

/Klavs

 
Jacob Storgaard Jensen
Reply

Hi Klavs,

As I wrote I'm new to razor... you write "And add the json.net nuget package to refrences" - what does this mean?

 
Sten Hougaard
Reply

Hej Jacob,

Prøv at se denne GIST: https://gist.github.com/netsi1964/863e8694658f06ae54f4

/Sten Hougaard

Twitter: @netsi1964

#hireMe - I am looking for a new job, so if you need an experienced front-end developer, please contact me: netsi1964@gmail.com

 
Klavs Martens
Reply

Hej Jacob

 

A nuget package is way to install extensions into a Visual Studio project. You only need to install it if you have a cutsom Dynamicweb solution without it, or if you running a really old version of Dynamicweb.

If you're running a standardl Dynamicweb Solution you don't need to install the json nugetpackage since its included by default.

Try the code I've  written in your razor template. It should work, and if not, send me the error message

Klavs

 
Jacob Storgaard Jensen
Reply

Hi Sten,

It's not quite what I'm looking for... well it can get the correct vimeo data, but I would like to have an item with a "Vimeo-Url" field, from which it finds the corresponding vimeo thumbnail. Otherwise my clients would have to add the module everytime they want to add a vimeo video to the gallery.

But caching is absolutely something that I would like to achieve too!

 
Jacob Storgaard Jensen
Reply

Hi Klavs,

I've tried that, but nothing shows up in my console... Solution is running: 8.5.1.25
This is what I've put in my template:


using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string url = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/76979871";
WebClient wc = new WebClient();
var json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString(url));
var thumbnail = json["thumbnail_url"].ToString();
Console.Write(thumbnail);
 
Sten Hougaard
Reply
This post has been marked as an answer

Hi Jacob,

Please revisit my Gist. I have added a paragraph Razor template to the gist, and the demo page now used that paragraph template to fetch the URL and show the thumbnail.

It should be converted to a template for an item, and then fetch the VIMEO URL from a item field. In my example I use the old style pattern where the paragraph image link field is used to specify the link to the Vimeo video :-)

GIST and again the demo

Oh, and I might mention that the only error you create in your razor template is to use console.write. That is only for console apps, and it makes no sense to use it a web context. To output something from Razor you could use for instance @thumbnail outside of the Razor code. You can see I have done it in my test razor template - the last line of code.

Hope that helps you, Jacob :-)

/Sten Hougaard

Twitter: @netsi1964

#hireMe - I am looking for a new job, so if you need an experienced front-end developer, please contact me: netsi1964@gmail.com

Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply

Hi Sten,

That was just what i wanted! Thanks a million! :-)

 

You must be logged in to post in the forum