两个几乎相同的功能,一个有效,另一个无效?

Two almost identical functions, one works, the other does not?

正在编写一个 C# 应用程序来与我们的 ServiceNow 对话 API。出于某种原因,我可以成功地忽略 JSON 文件的根令牌,它 returns 对用户而言,但对事件而言则不然。 JSON returns 除了一个来自事件 table 而另一个来自 sys_user table.[=15 之外,似乎几乎完全相同=]

在忽略根令牌的过程中尝试了几种不同的使用 Newtonsoft 的方法。这让我找到了这段代码现在的位置(尽管还未完善)

void incidentCreator(string incID)
{
    var restRequest = new RestRequest(_incTableLink + "/" + incID);
    restRequest.AddParameter("sysparm_display_value", "true");
    restRequest.AddParameter("sysparm_fields", "assigned_to,number,sys_updated_on,sys_id,comments_and_work_notes");
    string incidentJsonString = _restInteractions.querySnow(restRequest);
    Incident incident = null;
    try
    {
        incident = JObject.Parse(incidentJsonString).SelectToken("result[0]").ToObject<Incident>();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    _incidentList.Add(incident);
}

void userGetter(string userID)
{
    var restRequest = new RestRequest(_userTableLink);
    restRequest.AddParameter("sysparm_query", "sys_id=" + userID);
    string userJsonString = _restInteractions.querySnow(restRequest);
    User user = null;
    try
    {
        user = JObject.Parse(userJsonString).SelectToken("result[0]").ToObject<User>();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    //User user = JsonConvert.DeserializeObject<User>(userJsonString);
    _userList.Add(user);
}

JSON 事件字符串无效

"{
  \"result\": {
    \"number\": \"INC1215653\",
    \"sys_id\": \"52764be80007fb00903d51fea4ade50f\",
    \"comments_and_work_notes\": \"2019-08-01 10: 46: 19 - PERSON (Additional comments)\nEmailed PERSON 
admin of the site to check permissions\n",
    \"sys_updated_on\": \"2019-08-01 10: 46: 17\",
    \"assigned_to\": \"Employee\"
  }
}"  

JSON 有效的用户字符串

"{
  \"result\": [
    {
      \"calendar_integration\": \"1\",
      \"last_position_update\": \"\",
      \"u_lmc_account_indicator\": \"\",
      \"u_supply_chain\": \"false\",
    }
  ]
}"

无论我如何处理 incidentCreator() JSON 解析,使用 jsonconvert 或当前共享代码中的实现。我要么得到 "Object reference not set to an instance of an object.",要么得到一个所有变量都为空的对象。

有什么想法吗?对于被剥离的用户 json,我很抱歉,这是一个巨大的字符串,我懒得在发布之前清理它的 PII。

第二个JSON内容是一个数组。您可以在 result

一词后看到差异 [(数组)和 {(对象)

要解析案例 user,试试看

JArray a = JArray.Parse(json);
var listUser = new List<User>();
foreach (JObject o in a.Children<JObject>())
{
    foreach (JProperty p in o.Properties())
    {
        string name = p.Name;
        string value = (string)p.Value;
        var user = new User();
        user.Name = name;
        ...... 
    }
}

您的 incidentCreator 中的 JPath 表达式不正确。

应该是:-

try { incident = JObject.Parse(incidentJsonString).SelectToken("result").ToObject<Incident>(); }

此外,这可能只是将 JSON 粘贴到问题中的结果,但事件 JSON 示例的 "comments_and_work_notes" 字段未正确转义;只需使用 linter 检查您是否收到有效 JSON 即可。

不同之处在于,您的 JSON 事件字符串具有存储对象的字段 result,而用户的 JSON 字符串具有包含以下内容的字段 result一个对象的数组 事件

{
    result :
    {
        ...
    }
}

用户

{
    result :
    [
        {
            ...
        }
    ]
}

所以查询 SelectToken("result[0]") 将对用户有效,因为 result 字段包含一个元素位于索引 0 的数组,但它不会对事件起作用,因为 result 字段不包含数组。

但是 SelectToken("result").ToObject<Incident>() 应该给你你的 Incident 对象。