当我尝试解析Json结果时,出现以下异常:
解析值[。时遇到意外字符。路径“数据”,第1行,位置26。
这是我的Json数据:
{
"StatusCode":200,
"Data":[
{
"MemberId":1,
"CreaterUserId":5,
"FirstName":"Saravanan",
"LastName":"Subramanian",
"Title":"Physician\u0019s Assistant",
"License":"A 123447",
"Email":"saravanan.radhamani@gmail.com",
"PhoneNumber":"1234567",
"IsSign":true,
"IsMemberActive":true,
"CreatedDate":"2018-06-23T15:45:44.843",
"PhotoPath":null
}
]
}
这是我的C#代码:
public static void Main(string[] args)
{
string response = null;
try
{
response = makeRequest();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
var data = JsonConvert.DeserializeObject<PolstForm>(response);
Console.WriteLine(string.Format("StatusCode : {0} , Data : {1}",
data.StatusCode,
data.Data[0]));
Console.ReadKey();
}
private static string makeRequest()
{
var client = new RestClient(urlString);
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("accept", "application/json");
return client.Execute(request).Content; //the Content (body) of the response
}
这是我的PolstForm.class
public class PolstForm
{
public int StatusCode { get; set; }
public string Data { get; set; }
}
当我尝试从json解析数据时,会返回错误。
显示的错误是:
JsonReaderException未通过用户代码处理: Newtonsoft.Json.dll中发生了类型为'Newtonsoft.Json.JsonReaderException'的异常,但未在附加的用户代码中处理 信息:解析值时遇到意外字符:[。 路径“数据”,第1行,位置26。
响应包含上述Json格式。
如果您还有其他事情,请与我分享。
答案 0 :(得分:1)
您的PolstForm
类与您的Json的结构不匹配。
我会这样写的,例如:
public class PolstForm
{
public int StatusCode { get; set; }
public CustomData[] Data { get; set; }
}
public class CustomData
{
public int MemberId {get;set;}
public int CreaterUserId {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Title {get;set;}
public string License {get;set;}
public string Email {get;set;}
public string PhoneNumber {get;set;}
public bool IsSign {get;set;}
public bool IsMemberActive {get;set;}
public DateTime CreatedDate {get;set;}
public string PhotoPath {get;set;}
}
答案 1 :(得分:0)
如您所知, json字符串无法解析为字符串数据; [公共字符串数据{get;组; }]
[
{
"MemberId":1,
"CreaterUserId":5,
"FirstName":"Saravanan",
"LastName":"Subramanian",
"Title":"Physician\u0019s Assistant",
"License":"A 123447",
"Email":"saravanan.radhamani@gmail.com",
"PhoneNumber":"1234567",
"IsSign":true,
"IsMemberActive":true,
"CreatedDate":"2018-06-23T15:45:44.843",
"PhotoPath":null
}
]