2

PHP code:

$arr_return = array('status' => true,'expire' => $SQLGetUserResult['ExpirationDate'],'vip' =>  boolval($SQLGetUserResult['Vip']),'agent'=>true,'exception'=>false);
echo json_encode(array('result'=>$arr_return));

returns:

{"result":{"status":true,"expire":"2017-08-30 00:00:00","vip":false,"agent":true,"exception":false}}

C# deserialize json class using json to CSharp

public class Result
{
    public bool status { get; set; }
    public bool agent { get; set; }
    public string expire { get; set; }
    public bool vip { get; set; }
    public bool exception { get; set; }
}

public class RootObject
{
    public Result result { get; set; }
}

and deserialze

RootObject Ldata = JsonConvert.DeserializeObject<RootObject>(result);

throws:

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

I don't know why throw this exception?

Edit:
[Fiddle - Link code demo]

Edit: web source

i think this is not showing on code,string, blank string.. on result not showing VS 2017

Work now

i try search keep google and i found some solution using notepad ++ php file encoding change to utf-8 no bom option but login .php already utf-8(nobom) so i keep trying and find required php not correctly encoding thanks !

2
  • Your fiddle returns true, without any exception !? as I expected ;). Commented Aug 12, 2017 at 14:29
  • Your example is working. Check if you have problem with the escaping in your code. Commented Aug 12, 2017 at 14:32

1 Answer 1

2

Try this:

if (json.Substring(0, 1) != "{")
{
    json = json.Substring(json.IndexOf("{", StringComparison.Ordinal));
}

var result = JsonConvert.DeserializeObject<RootObject>(json);

Note: I think your json starts with some invisible characters.

Sign up to request clarification or add additional context in comments.

2 Comments

Does anyone know why this happens?
@Gohchi I think this happens because of PHP or at client-side, So; creating a proper fiddle that produces that json with additional character in PHP and asking a question with php may make some senses - the above code doesn't return that Sample Filddle - ;)

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.