Home > Enterprise >  error parsing json in asp.net mvc login project
error parsing json in asp.net mvc login project

Time:10-11

i am fetching data from an asp.net webapi and storing it as

var token = string.Empty;
var result = resMessage.Content.ReadAsStringAsync().Result;
token = JsonConvert.DeserializeObject<string>(result);

value of result is

{
value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwibmJmIjoxNjMzODY1NzE0LCJleHAiOjE2MzM4Njc1MTQsImlhdCI6MTYzMzg2NTcxNH0.2Vje7sXWw4tb_h50cR3zdI5TDIjOiWR-94_i2mH40cg",
formatters: [ ],
contentTypes: [ ],
declaredType: null,
statusCode: 200
}

the error i am getting

JsonReaderException: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.

in this line

token = JsonConvert.DeserializeObject<string>(result);

i am not able to understand what is going on. i have written this code in asp.net mvc project

CodePudding user response:

You're trying to deserialise a string into a string here, which won't work as they're the exact same type.

You need to deserialise string result into a concrete type, which JsonConvert.DeserializeObject should be aware of, to deserialise it as such.

This should work:

public class Data
{
    public string value { get; set; }
    public List<object> formatters { get; set; }
    public List<object> contentTypes { get; set; }
    public object declaredType { get; set; }
    public int statusCode { get; set; }
}
Data token = JsonConvert.DeserializeObject<Data>(result);
  • Related