在 C# 中访问 json.net jarray 中的项目

ano*_*opb 5 c# json json.net

我的 API 返回

{
  "result": [
    {
      "id": "51473",
      "name": "serv-vc",
      "modifydate": "2014-10-09 18:29:48.033",
      "expirationoff": "false",
      "createdate": "",
      "scheduleoff": "false",
    }
  ],
  "status": 0
}
Run Code Online (Sandbox Code Playgroud)

我将其存储为 JObject reponseobj

我无法弄清楚如何访问responseobj["result"][0]["id"].

每次我尝试这样做时,它都会给出一个关于越界的数组。

我缺少什么?

我也尝试过

JArray resultarr = (JArray)responseobj.SelectToken("result");
resultarr[0]["id"] 
Run Code Online (Sandbox Code Playgroud)

但有相同的结果。

Noc*_*tis 0

不确定你的问题是什么,但这似乎对我有用:

static void Main(string[] args)
{
    JObject j = JObject.Parse(
        "{\"result\": [{\"id\": \"51473\", \"name\": \"serv-vc\", \"modifydate\": \"2014-10-09 18:29:48.033\", \"expirationoff\": \"false\", \"createdate\": \"\", \"scheduleoff\": \"false\", } ], \"status\": 0 }" );
    var res = j["result"];
    Console.Out.WriteLine(res); 
    // show an arrays

    var maybe = j["result"][0];
    Console.Out.WriteLine(maybe);
    // shows the first object in the array

    var fail = j["result"][0]["id"];
    Console.Out.WriteLine(fail);
    // shows 51473
}
Run Code Online (Sandbox Code Playgroud)