在C#中访问json.net jarray中的项目 [英] accessing items in an json.net jarray in c#

查看:546
本文介绍了在C#中访问json.net jarray中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的api返回

{
  "result": [
    {
      "id": "51473",
      "name": "serv-vc",
      "modifydate": "2014-10-09 18:29:48.033",
      "expirationoff": "false",
      "createdate": "",
      "scheduleoff": "false",
    }
  ],
  "status": 0
}

我已存储为JObject reponseobj

which i've stored as a JObject reponseobj

我在弄清楚如何访问responseobj["result"][0]["id"]

每次我尝试它都会给出一个超出范围的数组.

every time i try that it gives an array about being out of bounds.

我想念什么?

我也尝试过

JArray resultarr = (JArray)responseobj.SelectToken("result");
resultarr[0]["id"] 

但结果相同.

推荐答案

假定响应位于名为response的字符串变量中,则可以这样做:

Assuming the response is in a string variable called response, this would do it:

JObject responseobj = JObject.Parse(response);
JObject result = (JObject)(responseobj.SelectToken("result") as JArray).First();
int id = result.Value<int>("id");

这篇关于在C#中访问json.net jarray中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆