Newtonsoft.Json: 从 JsonReader 读取 JObject 时出错。当前 JsonReader 项目不是对象:StartArray

Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray

您好,我在 var json = JObject.Parse(jStr); 行的代码中传递了以下字符串,但我收到错误

System.Private.CoreLib: Exception while executing function: Function1. Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray.

谁能解释一下我该如何解决这个问题。是因为它是数组类型吗?最后我传递的字符串将是一个数组。我怎样才能解决这个问题?。这是我在上面传递的示例字符串(数组中的一个对象)

[
  {
    "_id": "_AUQ9",
    "im:SiceErrors": {
      "errors": [
        {
          "_id": "O9UQ9_0",
          "ErrorMessage": "Uype",          
          "Parameter": "r:type",
          "ParameterValue": "[\n  \"~:bbl:P924\",\n  \"~:corer:ag\"\n]",
          "RefObjectParameter": "type_key",
          "Project": "NPOS+1"
        }
      ]
    },
    "cullad-tag:Cject": "P|GT",
    "bbl:P1014": "PIGT",
    "cullad-tag:MoomosBaseObjectDescription": "Gate Valve",
    "bbl:P3935": "Gve",
    "cullad-tag:CreateDate": "15.10.2021",
    "cullad-tag:MoomosDescription": "P2",
    "cullad-tag:MoomosUID": "A49",
    "bbl:011772": "UQ9",
    "cullad-tag:Description": "P2",
    "cullad-tag:Discipline": "U",
    "bbl:P101001445": "bbl:P163",
    "cullad-tag:FLabel": "FFeed",
    "bbl:P1094": "pd:P1299",   
    "bbl:P10791": "V",
    "cullad-tag:FutureTag": "No",
    "cullad-tag:IndexType": "MV",
    "bbl:P138": "bbl:P1563",
    "cullad-tag:IsTopTag": "No",
    "bbl:P7": "No",
    "cullad-tag:MountedOn": "F-18UD800",
    "bbl:P4024": "F-18800",   
    "bbl:P1834": "pd:P1094",    
    "bbl:P1803": "8120",
    "cullad-tag:SubProjectCategory": "Feed",
    "cullad-tag:SubProjectName": "SUB",
    "cullad-tag:System": "18",
    "bbl:P01660": "bbl:P15326",
    "cullad-tag:TagNumber": "F-120",
    "bbl:P10022": "F-20",   
    "cdf:type": [
      "bbl:P10924",
      "bbl:P101907",
      "bbl:P101003632",
      "bbl:P13932",
      "bbl:P21",
      "iso15926:InanimatePject",
      "iso15926:Object",
      "Moomos:PopoFeedTag"
    ],
    "primarycdf:type": "bbl:P924"
  }
]

您必须将字符串包装起来才能成为有效的 json 对象:

var json = JObject.Parse("{ \"Data\":" + jStr + "}");

生成的对象将包含 属性 Data 以及来自 json

的数据

由评论编辑: 或者试试这个方法:

foreach(JObject obj in JArray.Parse(jStr))
{
    // todo: pass an object to a method that requires an JObject to process
}

您的示例是数组中的单个对象。

@SeeSharp 建议包装那个数组。

相反的方法是,如果情况总是如此,您可以只解析该数组中的对象(删除第一个和最后一个字符):

var json = JObject.Parse(jStr.Remove(str.Length - 1).Remove(0, 1));

否则,我猜你只是调整你的代码来处理一个数组,而不是一个对象(这看起来是更好的方法)

编辑:

由于您的 JArray 中有多个对象,只需对其进行迭代即可:

foreach (JObject item in JArray.Parse(jStr)) 
{
  DoStuffOnJObject(item) // call your method
}