cherry
cherry

Reputation: 37

Newtonsoft.Json Deserialize issue using php 7.0 json_encode array string

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 !

Upvotes: 2

Views: 1399

Answers (1)

shA.t
shA.t

Reputation: 16968

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.

Upvotes: 2

Related Questions