简体   繁体   English

如何使用 C# 从 php 调用 json_encode 函数到 asp.Net?

[英]How to call json_encode function from php to asp.Net with C#?

I am trying to call PHP file containing information in JSON format.我正在尝试调用包含 JSON 格式信息的 PHP 文件。

Here is my code from Request.php这是我来自 Request.php 的代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
session_start();

$conn =  mysqli_connect("localhost", "root","akshat", "staff");
                                if ($conn === false) {
                                    die("Connection failed: " . $conn->connect_error);
                                } 
$sqlgrp="select staff.groupid from staff.STAFF where staff.NAME='".$_SESSION['username']."'";
$resultgrp = mysqli_query($conn, $sqlgrp);
    if (mysqli_num_rows($resultgrp) > 0) {
    while($rowgrp = mysqli_fetch_assoc($resultgrp)) {
    $grp=$rowgrp['groupid'];
                }
    }

$myObj->name = $_SESSION["username"];
$myObj->branchno = $_SESSION["branch"];
$myObj->group = $grp;

$myJSON = json_encode($myObj);

echo $myJSON;
?>
</body>
</html>

Here's my c# code :这是我的 C# 代码:

public void GetResult(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            string jsonstring = string.Empty;
            try
            {
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())


 {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                jsonstring = reader.ReadToEnd();
                string compiled = Regex.Match(jsonstring, "(?s)<body[^>]*>(.*)</body>").Groups[1].Value;
                jsonString.Text = JsonConvert.DeserializeObject(compiled).ToString();
            }
        }
        catch (Exception ex)
        {
            error.Text = ex.Message;
        }
    }

When I run php in browser it shows values当我在浏览器中运行 php 时,它会显示值

when making request from c# I get,从 c# 发出请求时,我得到,

{ "name": null, "branchno": null, "group": "1" }

What could have gone possibly wrong?可能出了什么问题?

How to get values in Json Format?如何获取 Json 格式的值? All I get is null.我得到的都是空的。

As per your shared json you are getting null in name and branhno but group is having value.根据您共享的 json,您的名称和 branhno 为null ,但组具有价值。

{ "name": null, "branchno": null, "group": "1" }

I think you need to check and validate rather this session variable is having value when your are setting it to the name and branch attribute.我认为您需要检查和验证这个会话变量是否在您将其设置为 name 和 branch 属性时具有值。

$myObj->name = $_SESSION["username"];
$myObj->branchno = $_SESSION["branch"];

and if session has value then the issue is in json decode function.如果 session 有价值,那么问题出在 json 解码函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM