- Home /
Error: JSON parse error: Invalid value.
First I apologize. I am using google translate to ask the question.
I am trying to make a logging system in a mysql database with unity. At the moment, the database is local. But when sending the data, or rather when receiving it, the following error occurs:
ArgumentException: JSON parse error: Invalid value.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <3e7f43c38e214491a21ee815b941dbb6>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <3e7f43c38e214491a21ee815b941dbb6>:0)
NetworkManager+<CO_CreateUser>d__1.MoveNext () (at Assets/Scripts/NetworkManager.cs:30)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <9899854e5b05425ab27d31f737cde095>:0)
Registration should occur when the user presses a button. The method that is executed when the button is pressed is in a script called MainMenuController. The method is the following:
public void SubmitRegistration(){
if (nameCountrySelected == ""){
UserError(3);
return;
}
if(!EmptyInputField()){
UserError(1);
return;
}
if (inpPasswordRegistrationMenu.text != inpConfirmPasswordRegistrationMenu.text){
UserError(2);
return;
}
CommunicationMenu.SetActive(true);
networkManager.CreateUser(inpNameRegistrationMenu.text, inpEmailRegistrationMenu.text,
inpPasswordRegistrationMenu.text, inpNickNameRegistrationMenu.text, nameCountrySelected,
delegate (Response response){
txtComm.text = response.message;
}
);
}
Where txt Comm.text is a TEXTFIELD that should display the message it receives. The script that the "Assets/Scripts/NetworkManager.cs:30" error refers to is as follows:
public class NetworkManager : MonoBehaviour{
public void CreateUser(string UserName, string email, string pass, string nickName, string country, Action <Response> response){
StartCoroutine(CO_CreateUser(UserName, email, pass, nickName, country, response));
}
private IEnumerator CO_CreateUser(string UserName, string email, string pass, string nickName, string country, Action<Response> response){
WWWForm form = new WWWForm();
form.AddField("UserName", UserName);
form.AddField("email", email);
form.AddField("pass", pass);
form.AddField("nickName", nickName);
form.AddField("contry", country);
UnityWebRequest www = UnityWebRequest.Post(GameConstants.URL_ADDRESS_CREATE_USER, form);
yield return www.SendWebRequest();
response(JsonUtility.FromJson<Response>(www.downloadHandler.text));
if (www.result != UnityWebRequest.Result.Success) //www.result
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
[Serializable] public class Response{
public bool done;
public string message;
}
It is on line 30 where the error occurs.
response(JsonUtility.FromJson<Response>(www.downloadHandler.text));
The php code that is executed:
<?php
include "dbConnection.php";
include "ConstantsFile.php";
include "functions.php";
$UserName = $_POST['UserName'];
$email = $_POST['email'];
$pass = hash("sha256", $_POST['pass']);
$nickName = $_POST['nickName'];
$contry = $_POST['contry'];
$sql = "SELECT Name_User FROM users WHERE Name_User = '$UserName'"
$result = $pdo->query($sql);
if($result->rowCount() > 0){
MessageAfterQuery(false, ERROR_NAME_USER);
exit();
}
else{
$sql = "SELECT Email_User FROM users WHERE Email_User = '$email'"
$result = $pdo->query($sql);
if($result->rowCount() > 0){
MessageAfterQuery(false, ERROR_EMAIL);
exit();
}
}
else{
$sql = "INSERT INTO users(Name_User, Email_User, Password_User,
Nick_User, Country_User)
VALUES ('[$UserName]','[$email]','[$pass]','[$nickName]','[$contry]')";
$pdo->query($sql);
MessageAfterQuery(true, CONFIRM_CREATE_USER);
exit();
}
?>
The function that must send the information in Json "MessageAfterQuery" is this:
<?php
include "ConstantsFile.php";
$data ="";
function MessageAfterQuery($boolConfirm, $messageQuery){
$data = array('done'=> $boolConfirm, 'message'=> $messageQuery);
Header('Content-Type: application/json');
echo json_encode($data);
exit();
}
?>
I have searched the internet and I have tried different alternatives but I cannot solve this error.
I don't know what part of the code is wrong.