Json Line error 0 position 0

Asked

Viewed 63 times

0

I have looked here on the website about this error and so far I have not found anything to solve my problem. I’m trying to use restsharp, but I’m getting an error when passing the data to . json

My Page with API

<?php
header('Content-type: application/json');

require_once("config/config.php");

$result = array('status' => 500, 'message' => "Internal error");

if (isset($_POST['login']) && isset($_POST['password']))
    {
		$login = $_POST['login'];
        $password = $_POST['password'];
        $uuid = 'Not Found';
		
		if (validaUsuario($login, $password) == true) 
		{
            $result['status'] ='42';
            $result['message'] = 'Connected with success';
			$result['user'] = 'Teste';
            $result['level'] = '100';			

			echo json_encode($result);
            return;
			
        } else 
		{
			$result['status'] =202;
            $result['message'] = "Password or login incorrect";
			$result['token'] = 123456789;
            $result['level'] = 10;
			echo json_encode($result);
            return;
		}
            
			echo json_encode($result);

	}

?>

My code:

public static void UserLogin(string user,string pass)
    {
        var client = new RestClient();
        client.BaseUrl = new Uri("http://localhost/r_user.php");

        var request = new RestRequest();
        request.Method = Method.POST;

        request.AddParameter("login", user);
        request.AddParameter("password", pass);

        IRestResponse response = client.Execute(request);
        var content = response.Content;

        dynamic res = JObject.Parse(content);

        if (res.status == "42")
        {

        }
    }

Returns me the following error:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: . Path '', line 0, position 0.'

I’m getting content like this : {"status":"42","message":"Connected with success","user":"Teste","level":"100"}

inserir a descrição da imagem aqui

  • $result =(Object) ['status' => 500, 'message' => 'Internal error']; has tried to use so?

  • I tried but it didn’t work either @Will

  • what is the error? your C# method waits user and pass but you put login and password

  • @Leandroangelo Obridado for the help,I managed to solve, posted the resolution of the problem

1 answer

0


After several tests, I discovered that it was an easy problem to solve.

I was receiving the visually correct data, copied the return in json that I was receiving and put in json validator

In this validator I realized that there was a space before the first key, to deserialize can not have a space in the front. I was using Notepad++ and changed the encoding to UTF8-No GOOD, but the problem remained, studying further realized that all the files that my.php file uses need is with the same encoding.

In my case I called another file:

require_once("config/config.php");

I changed the config.php condition and the problem solved it.

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.