In: Computer Science
C#
I need working code please
Write a console application that accepts the following JSON as input:
{"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View SVG"}, {"id": "ViewSource", "label": "View Source"}, {"id": "SaveAs", "label": "Save As"}, null, {"id": "Help"}, {"id": "About", "label": "About Adobe CVG Viewer..."} ] }}
Then deserialize it, convert it to XML, and print out the result. It should look like the following:
<menu> <header>Adobe SVG Viewer</header> <item action="Open" id="Open">Open</item> <item action="OpenNew" id="OpenNew">Open New</item> <separator/> <item action="ZoomIn" id="ZoomIn">Zoom In</item> <item action="ZoomOut" id="ZoomOut">Zoom Out</item> <item action="OriginalView" id="OriginalView">Original View</item> <separator/> <item action="Quality" id="Quality">Quality</item> <item action="Pause" id="Pause">Pause</item> <item action="Mute" id="Mute">Mute</item> <separator/> <item action="Find" id="Find">Find...</item> <item action="FindAgain" id="FindAgain">Find Again</item> <item action="Copy" id="Copy">Copy</item> <item action="CopyAgain" id="CopyAgain">Copy Again</item> <item action="CopySVG" id="CopySVG">Copy SVG</item> <item action="ViewSVG" id="ViewSVG">View SVG</item> <item action="ViewSource" id="ViewSource">View Source</item> <item action="SaveAs" id="SaveAs">Save As</item> <separator/> <item action="Help" id="Help">Help</item> <item action="About" id="About">About Adobe CVG Viewer...</item> </menu>
The program is given below. The comments are provided for the better understanding of the logic. Please note that for parsing, Newtonsoft.Json is used and it has to be installed from Nuget Manager for this solution to work, in the Visual Studio. It is also assumed that the input comes from the command line as the 1st argument. If there is any other mechanism of providing input, the statement "string jsonInput = args[0];" need to be altered. Also since the json input string is multiline and it has double quotes within it, it has to be formatted before passing it to the program (applicable only if it is passing from the command line).
using System;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
public class JSONHandler
{
public static void Main(string[] args)
{
//Pass the json as the 1st parameter from the command line.
string jsonInput = args[0];
//Initialise Newtonsoft JSON Converter
//This requires Newtonsoft.Json package to be installed from the Nuget Manager
dynamic parsedJsonObject = JsonConvert.DeserializeObject(jsonInput);
//The string xmlFormattedString will hold the result xml
string xmlFormattedString = "<menu>\n";
//Parse the header and store it.
xmlFormattedString = xmlFormattedString + String.Format("\t<header>{0}</header>\n", parsedJsonObject.menu["header"]);
///Loop through the items.
foreach (Newtonsoft.Json.Linq.JObject items in parsedJsonObject.menu["items"])
{
string id = "";
string label = "";
//If items is null, it doesn't have any entry and a separator tag need to be inserted in the xml.
//and continue with the next iteration of the loop.
if (items is null)
{
xmlFormattedString = xmlFormattedString + "\t<separator/>\n";
}
else if (items.HasValues)
{
if(items.Count == 1)
{
//Check if the sub items are 1. If so, only id exists and use that as id and label.
id = items["id"].ToString();
label = items["id"].ToString();
}
else
{
//If more than 1 sub items are present, id and label exists and extract that.
id = items["id"].ToString();
label = items["label"].ToString();
}
//format the xml using id and label.
xmlFormattedString = xmlFormattedString + String.Format("\t<item action=\"{0}\" id=\"{0}\">{1}</item>\n", id, label);
}
}
xmlFormattedString = xmlFormattedString + "</menu>";
//Print the formatted xml.
Console.WriteLine(xmlFormattedString);
Console.ReadLine();
}
}
The screenshots of the code is provided below.
The input has to be supplied to the program as below. Go to the Project Properties for setting this.
"{'menu': {
'header': 'SVG Viewer',
'items': [
{'id': 'Open'},
{'id': 'OpenNew', 'label': 'Open New'},
null,
{'id': 'ZoomIn', 'label': 'Zoom In'},
{'id': 'ZoomOut', 'label': 'Zoom Out'},
{'id': 'OriginalView', 'label': 'Original View'},
null,
{'id': 'Quality'},
{'id': 'Pause'},
{'id': 'Mute'},
null,
{'id': 'Find', 'label': 'Find...'},
{'id': 'FindAgain', 'label': 'Find Again'},
{'id': 'Copy'},
{'id': 'CopyAgain', 'label': 'Copy Again'},
{'id': 'CopySVG', 'label': 'Copy SVG'},
{'id': 'ViewSVG', 'label': 'View SVG'},
{'id': 'ViewSource', 'label': 'View Source'},
{'id': 'SaveAs', 'label': 'Save As'},
null,
{'id': 'Help'},
{'id': 'About', 'label': 'About Adobe CVG Viewer...'}
]
}}"
The output screenshot is provided below.
The Nuget package can be installed as follows.