Click here to Skip to main content
15,890,825 members
Articles / Desktop Programming / MFC
Tip/Trick

MFC PowerShells Easily: JSON to HTM (Motivated by a Forum Question)

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
28 Feb 2015CPOL2 min read 8.5K   78  
A simple, short and easy way to display a JSON object in a browser (motivated by a forum question)

Introduction

The purpose of this trick/tip is demonstrating how easy it is to use the MFC class CPowerShell from http://www.codeproject.com/Articles/880154/MFC-PowerShells-Easily for tasks that require otherwise extensive and many times complicated code.

The motivation of this trick/tip is this forum question.

Using the CPowerShell MFC class from the article above makes this task really simple.

Using the Code

I attached a console application solution (with MFC support) to demonstrate the simplicity of this solution (but you must download the CPowerShell class header file from the article mentioned in the introduction).

The solution to this forum question is so simple that it only takes only 3 C++/MFC statements (in addition to the CPowerShell class).

Here they are:

C++
CString json = _T(" ... "); // I omitted the string
CPowerShell posh;
posh.Run =
    _T("$json='")
    + json +
    _T("'\r\n")
    _T("($json|convertfrom-json).fields|convertto-html >($htm=$env:tmp+'/json2html.htm')\r\n")
    _T("ii $htm")
    ;

Observation: I checked and re-checked the C++ code above, and there's no error, no typo, etc.

That's all!

Please, notice that I omitted the JSON string, because it is a pretty long one, but you can see it in a project file at the top. What I can tell you in advance is that it's pretty ugly: full of escaped characters.

Update: I've just had another look at this JSON string... don't look at it, at least without medical assistance- it's really pretty ugly.

In the code above, the JSON objects are converted in PSObjects, and then, displayed in the default browser.

Points of Interest

CROM technology (see my previous article, already mentioned) borrows a lot of power from other applications, just like COM technology.

Thank you PowerShell Team, thank you to the guy who created this acronym CROM, because now, everything is easier when using MFC.

Honestly, PowerShell adds so much to MFC, that I think MFC Team should add a PowerShell class of their own, with much more power, after all, they can ask features to the PowerShell team.

History

  • 27th February, 2015 - Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --