0

I'm receiving this error when trying to read a JObject in C# from PHP, it is the result of a basic query "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;
}

What am I doing wrong? Thanks.

4
  • Could you tell us what is in the JSON variable? Commented Jul 13, 2012 at 14:00
  • You should probably see the output phpResponse yourself by adding a breakpoint to see if it is really valid JSON Commented Jul 13, 2012 at 14:03
  • this is the same database, same query topofsteel.com/PHP/test-database.php Commented Jul 13, 2012 at 14:08
  • When does $_POST["test"] become a query? Commented Jul 15, 2012 at 18:19

2 Answers 2

2

The problem is that Answer.ToString() won't return the stream contents as a string. Try something like this.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

It turns out the problem was the array type.

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

does not work, but

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

and

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

do. Does anyone have any idea why? Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.