Last active
October 22, 2018 19:42
-
-
Save jgwill/d0bc8d1d6f0b5a90be67c8d1892e599f to your computer and use it in GitHub Desktop.
@stcgoal 18101513 Node.JS and .NET Integrated. This grab the output from a nodejs and transform it into C# object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//sample JSON output that c# grab and transform | |
console.log('{"name":"guillaume","phone":"8195344332"}'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
//This grab the output from a nodejs and transform it into C# object | |
namespace dnc_nodejs_001 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Console.WriteLine("Hello World!"); | |
var proc = new System.Diagnostics.Process(); | |
proc.StartInfo.CreateNoWindow = true; | |
proc.StartInfo.RedirectStandardInput = true; | |
proc.StartInfo.RedirectStandardOutput = true; | |
proc.StartInfo.UseShellExecute = false; | |
proc.StartInfo.RedirectStandardError = true; | |
proc.StartInfo.FileName = "node"; | |
proc.StartInfo.Arguments = "app.js"; | |
proc.Start(); | |
proc.BeginOutputReadLine(); | |
proc.StandardInput.WriteLine("2 + 2;"); | |
proc.StandardInput.WriteLine("setTimeout(function(){ process.exit();}, 10000).suppressOut;"); | |
proc.OutputDataReceived += proc_OutputDataReceived; | |
proc.WaitForExit(); | |
} | |
static void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) | |
{ | |
string mydata = e.Data.ToString(); | |
System.Console.WriteLine("The data received is : " + mydata); | |
MyMessage yourObject = JsonConvert.DeserializeObject<MyMessage>(mydata); | |
System.Console.WriteLine("Converted... \n"+ | |
"Name: " + yourObject.Name +"\n" | |
+ "Phone: " + yourObject.Phone | |
); | |
} | |
} | |
public class MyMessage | |
{ | |
[JsonProperty("name")] | |
public string Name { get; set; } | |
[JsonProperty("phone")] | |
public string Phone { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment