解析值{时遇到意外字符。路径

时间:2019-03-01 15:11:12

标签: c# json asp.net-mvc

试图获取Json的值,但不断抛出此错误。

enter image description here

这是代码:

string djson = JsonConvert.SerializeObject(e);


            var httpContent = new StringContent(djson, Encoding.UTF8, "application/json");

            var response = await client.PostAsync(Baseurl + "ex.api/api/exmaple/indi/getindicertification", httpContent);
            var jsonString = await response.Content.ReadAsStringAsync();
            var messageResponse = JsonConvert.DeserializeObject<Messages>(jsonString);
            var responseData = JsonConvert.DeserializeObject<ResultingRequest>(jsonString);

我也尝试过这种方法,但是也让我感到错误:

var responseData = JsonConvert.DeserializeObject<List<ResultingRequest>>(jsonString);

我的模型类如下:

public class ResultingRequest : EvaluationResult
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public DateTime RequestDate { get; set; }
    public int CertificationRequestStatus { get; set; }
    public int Language { get; set; }
    public string OGPCorrelationID { get; set; }
    public DateTime EmissionDate { get; set; }
    public object Reason { get; set; }
    public object Owner { get; set; }
    public string OGPATGNumber { get; set; }
    public object SolicitantName { get; set; }
    public object SolicitantIDNumber { get; set; }
    public DateTime ExpirationDate { get; set; }
    public bool CommitedToPRACSES { get; set; }
    public object Resolution { get; set; }
    public object Kiosco { get; set; }
    public object Comments { get; set; }
    public bool PorAcuerdo { get; set; }
    public int KioscoId { get; set; }
    public string EvaluationResult { get; set; }
}

这是Json:

{
"ResultingRequest": {
    "Id": 123 ,
    "UserId": 3456 ,
    "RequestDate": "09-20-2019",
    "CertificationRequestStatus": 4,
    "Language": 1,
    "OGPCorrelationID": "",
    "EmissionDate": "",
    "Reason": null,
    "Owner": null,
    "OGPATGNumber": "",
    "SolicitantName": null,
    "SolicitantIDNumber": null,
    "ExpirationDate": "",
    "CommitedToPRACSES": false,
    "Resolution": null,
    "Kiosco": null,
    "Comments": null,
    "PorAcuerdo": false,
    "KioscoId": 1,
    "EvaluationResult": "Negative"
},
"EvaluationResult": {
    "SolicitantIsMinor": false,
    "EvaluationResults": [],
    "IndividualHasNoCases": true,
    "IndividualDeceased": false,
    "CertificationType": "Negative",
    "RestrictedIndividual": false
},
"CertificationPDFBytes": "",
"SentByEmail": true,
"SentByEmailSuccess": true,
"Message": null,
"CertificationKey": ""
}

我想从ResultingRequest:{}中获取值“ EvaluationResult”。我做错了什么或我错过了什么?我尝试了几种搜索方法,但是所有方法仍然会收到错误消息。

2 个答案:

答案 0 :(得分:1)

首先,如果您反序列化为列表,则json请求应该是一个ResultingRequest数组,现在他不是ResultingRequest数组,因为它不是一个json对象,您必须将[]添加到您的json请求中。 示例:

[{
"ResultingRequest": {
    "Id": "",
    "UserId": "",
    "RequestDate": "",
    "CertificationRequestStatus": 4,
    "Language": 1,
    "OGPCorrelationID": "",
    "EmissionDate": "",
    "Reason": null,
    "Owner": null,
    "OGPATGNumber": "",
    "SolicitantName": null,
    "SolicitantIDNumber": null,
    "ExpirationDate": "",
    "CommitedToPRACSES": false,
    "Resolution": null,
    "Kiosco": null,
    "Comments": null,
    "PorAcuerdo": false,
    "KioscoId": 1,
    "EvaluationResult": "Negative"
}}]

第二,其余的json将不会被解析,因为它们不是ResultingRequest对象,最后就像他们在这里提到的那样,您没有指定某些属性值

编辑:由于您说的是请求是由API发送的,并且您不想更改json格式,因此可以执行以下操作,将JSON解析为JObject(因为json是对象,而不是数组),然后选择要检索的对象(在本例中为ResultingRequest),然后反序列化,如下所示:

     var parsedObject = JObject.Parse(jsonString);
     var resultingRequestJson= parsedObject["ResultingRequest"].ToString();
     var responseData = JsonConvert.DeserializeObject<ResultingRequest>(resultingRequestJson);

我为您的json文件制作了一个简单的示例,并遇到了其他一些进一步的错误,为了解决该问题,我将Datetime属性更改为可空值,因为您的json文件返回了空字符串示例:

public DateTime? EmissionDate { get; set; }

答案 1 :(得分:0)

您的Json是错误的,请注意Json中的第一个属性(IdUserId缺少引号) 尝试以这种方式使用它:

[{
  "ResultingRequest": {
    "Id": "",
    "UserId": "",
    "RequestDate": "",
    "CertificationRequestStatus": 4,
    "Language": 1,
    "OGPCorrelationID": "",
    "EmissionDate": "",
    "Reason": null,
    "Owner": null,
    "OGPATGNumber": "",
    "SolicitantName": null,
    "SolicitantIDNumber": null,
    "ExpirationDate": "",
    "CommitedToPRACSES": false,
    "Resolution": null,
    "Kiosco": null,
    "Comments": null,
    "PorAcuerdo": false,
    "KioscoId": 1,
    "EvaluationResult": "Negative"
  },
  "EvaluationResult": {
    "SolicitantIsMinor": false,
    "EvaluationResults": [],
    "IndividualHasNoCases": true,
    "IndividualDeceased": false,
    "CertificationType": "Negative",
    "RestrictedIndividual": false
  },
  "CertificationPDFBytes": "",
  "SentByEmail": true,
  "SentByEmailSuccess": true,
  "Message": null,
  "CertificationKey": ""
}]