【发布时间】:2012-10-17 02:08:22
【问题描述】:
我正在尝试将以下代码 sn-p 从 PHP 转换为 C# 或 VB.NET 这是来自用于从外部 webhook 捕获 JSON 字符串的 PHP 页面。
// 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!
}
}
}
}
ETA:我现在从流中得到了 json 字符串。仍在试图弄清楚如何解析这个。 JSON 字符串格式不在我的控制范围内,但我基本上需要获取“属性”节点。
【问题讨论】:
-
只是谷歌“打开并读取文件 c#”...
-
你走了多远?发布一些您尝试过的 C#/VB.NET 代码
-
还没有走得很远,因为我无法通过 file_get_contents('php://input'')。我正在尝试使用 System.Net.WebClient
-
移植代码通常很容易。移植一个库(又名框架、dll、api)可能非常困难。您正在尝试移植库(特别是嵌入在 PHP 框架中的函数)。我认为您不会找到一种简单或有用的方法来做到这一点。
-
@WhiskerBiscuit 如果您想将我的评论标记为您的问题的解决方案,我已将其转化为答案。