如何解码ASPX页面中的JSON字符串?

时间:2015-05-05 18:00:49

标签: php asp.net json curl

我通过aspx将json格式的数据从我的服务器发布到远程服务器 页面,如下所示:

function httpPost($url, $params) {

    $payload = json_encode($params);
    //echo $payload;die;
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POST, count($payload));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload))
    );

    $output = curl_exec($ch);

    curl_close($ch);
    return $output;
    //return json_decode($output, true);
}

$params = array(
    "type" => "1",
    "ApplicationNo" => "1234567",
    "Effdate" => '05-06-1987',
    "InstituionID" => 'E01',
    "CourseCode" => '091',
);

echo httpPost("http://someurl/RESPONSE.aspx", $params);

如何解析aspx页面中的json字符串?

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容获取该值(JSON)字符串。

StreamReader strmreader = new StreamReader(Request.InputStream);
string json_value = strmreader.ReadToEnd();

然后你需要创建一个具有匹配JSON键的类(如下所述),并使用该类反序列化json_value,这将获得值。

public class JSONReceived
{
    public string type { get; set; }
    public string ApplicationNo { get; set; }
    public string Effdate { get; set; }
    public string InstituionID { get; set; }
    public string CourseCode { get; set; }
}

然后使用以下反序列化。

System.Web.Script.Serialization.JavaScriptSerializer jsz = new System.Web.Script.Serialization.JavaScriptSerializer();
JSONReceived r_obj = jsz.Deserialize<JSONReceived>(json_value);

现在,您可以使用此r_obj.type等来访问json值...