将Json从PHP读入C#时出错

时间:2012-07-13 13:59:14

标签: c# php json

我尝试从JObject读取C#中的PHP时收到此错误,这是基本查询的结果"SELECT * FROM items"...

Unexpected character encountered while parsing value: S. Path '', line 0, position 0.

PHP

$query = ($_POST["test"]);

if ($result = $mysqli->query($query)
{
    $jsonResult = json_encode($result);
}   

echo $jsonResult;

C#

public JObject GetThat()
{
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

    Stream Answer = WebResp.GetResponseStream();

    string phpResponse = Answer.ToString();
    JObject myResult = JObject.Parse(phpResponse);

    return myResult;
}

我做错了什么?感谢。

2 个答案:

答案 0 :(得分:2)

问题是Answer.ToString()不会将流内容作为字符串返回。尝试这样的事情。

public JObject GetThat()
{
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

    string phpResponse = string.Empty;
    using(StreamReader rdr = new StreamReader(WebResp.GetResponseStream()))
        phpResponse = rdr.ReadToEnd();
    }

    JObject myResult = JObject.Parse(phpResponse);

    return myResult;
}

答案 1 :(得分:0)

事实证明问题是数组类型。

$row = $result->fetch_array(MYSQLI_NUM);

不起作用,但

$row = $result->fetch_array(MYSQLI_BOTH);

$row = $result->fetch_array(MYSQLI_ASSOC);

做。有谁知道为什么?感谢。