C# 将PHP代码段转换为C或VB.NET
我正在尝试将下面的代码片段从PHP转换为C或VB.NET,这是一个PHP页面,用于从外部webhook捕获JSON字符串C# 将PHP代码段转换为C或VB.NET,c#,php,vb.net,json,C#,Php,Vb.net,Json,我正在尝试将下面的代码片段从PHP转换为C或VB.NET,这是一个PHP页面,用于从外部webhook捕获JSON字符串 // Get the POST body from the Webhook and log it to a file for backup purposes... $request_body = file_get_contents('php://input'); $myFile = "testfile.txt"; $fh = fopen($myFile, 'w') or di
// Get the POST body from the Webhook and log it to a file for backup purposes...
$request_body = file_get_contents('php://input');
$myFile = "testfile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $request_body);
fclose($fh);
// Get the values we're looking for from the webhook
$arr = json_decode($request_body);
foreach ($arr as $key => $value) {
if ($key == 'properties') {
foreach ($value as $k => $v) {
foreach ($v as $label => $realval) {
if ($label == 'value' && $k == 'zip') {
$Zip = $realval;
}
elseif($label == 'value' && $k == 'firstname') {
$Fname = $realval;
}
elseif($label == 'value' && $k == 'lastname') {
$Lname = $realval;
}
elseif($label == 'value' && $k == 'email') {
$Email = $realval;
}
elseif($label == 'value' && $k == 'phone') {
$Phone = $realval;
$Phone = str_replace("(", "", $Phone);
$Phone = str_replace(")", "", $Phone);
$Phone = str_replace("-", "", $Phone);
$Phone = str_replace(" ", "", $Phone);
}
//need the other values as well!
}
}
}
}
埃塔:我已经从流中得到了json字符串。仍在试图找出如何解析这个。JSON字符串格式超出了我的控制范围,但我基本上需要获取properties节点。.NET的基本库没有任何处理JSON输入的好方法。相反,请看一看,这是一个高性能的第三方库,可以满足这一需求 链接页面上有一些用法示例。将为您指明如何将流写入文件的正确方向。在您的例子中,流是php://input.
要处理JSON部分,请查看@YYY的答案。如果我理解正确,这里基本上就是您要做的。但正如其他人提到的那样。JSON.NET是更好的选择
private void Request()
{
//Makes Request
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/Test.php");
request.ContentType = "application/json; charset=utf-8";
request.Accept = "application/json, text/javascript, */*";
request.Method = "POST";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write("{id : 'test'}");
}
//Gets response
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
string json = "";
using (StreamReader reader = new StreamReader(stream))
{
//Save it to text file
using (TextWriter savetofile = new StreamWriter("C:/text.txt"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
savetofile.WriteLine(line);
json += line;
}
}
}
//Decodes the JSON
DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(MyCustomDict));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
MyCustomDict dict = (MyCustomDict)dcjs.ReadObject(ms);
//Do something with values.
foreach(var key in dict.dict.Keys)
{
Console.WriteLine( key);
foreach(var value in dict.dict[key])
{
Console.WriteLine("\t" + value);
}
}
}
[Serializable]
public class MyCustomDict : ISerializable
{
public Dictionary<string, object[]> dict;
public MyCustomDict()
{
dict = new Dictionary<string, object[]>();
}
protected MyCustomDict(SerializationInfo info, StreamingContext context)
{
dict = new Dictionary<string, object[]>();
foreach (var entry in info)
{
object[] array = entry.Value as object[];
dict.Add(entry.Name, array);
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (string key in dict.Keys)
{
info.AddValue(key, dict[key]);
}
}
}
这要归功于既然你们这些无情的暴徒对我下手,我觉得我有义务至少记录下我的进步
Using inputStream As New StreamReader(Request.InputStream)
JSON = inputStream.ReadToEnd
If JSON.Length > 0 Then
Using writer As StreamWriter = New StreamWriter("c:\temp\out.txt")
writer.Write(JSON)
End Using
End If
End Using
只要谷歌打开并读取文件c…你已经走了多远?发布一些C/VB.NET代码,这些代码你尝试得还不太深入,因为我无法找到通过文件内容的方法php://input. 我正在尝试使用System.Net.WebClient端口代码通常非常简单。移植库aka框架、dll、api可能非常困难。您正在尝试移植库,这是一个嵌入在PHP框架中的函数。“我认为你找不到一个简单或有用的方法来做这件事。@如果你想把我的评论作为你问题的解决方案,我已经把它变成了一个答案。我有点被自定义词典卡住了。”。您的代码正在部分反序列化,但JSON的内部属性似乎丢失了JSON来自外部站点,并且它似乎在数组中包含数组。不知道如何在这里发布它,因为它是一个消息。不幸的是,如果没有您的直接代码,我没有适当的设置来查看这个JSON。但我认为这将是一个修改MyCustomDict的问题,以便在foreach循环中查找嵌入的数组。您可能想看看@ConradFrix中的6。感谢您为我指出这篇文章,因为其中的指导原则非常有用。但我不确定这里的情况是否是6。我链接到的答案只是为OP提供了一个起点,它只提供了这里所问问题的部分解决方案。如果我误解了什么,请告诉我。