在Json响应中查找Specfic值

时间:2016-08-30 10:42:46

标签: c# json

我试图从Json响应中捕获一个值,我尝试了一系列方法,但似乎没有任何工作,这是我到目前为止所做的。

var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
{
    var responseText = streamReader.ReadToEnd();
    Console.Write(responseText);
    var data = (JObject)JsonConvert.DeserializeObject(responseText);
    var name = data["number"];
    Console.WriteLine("Version Number" + name);

Json,回复:

{{  "id": "58523699",  "type": "page",  "status": "current",  "title": "3 - UAT Environments",  "space": {    "id": 58949633,    "key": "SysEnv",    "name": "System Environments",    "type": "global",    "_links": {      "webui": "/display/SysEnv",      "self": "https://navmanwireless.atlassian.net/wiki/rest/api/space/SysEnv"    },    "_expandable": {      "metadata": "",      "icon": "",      "description": "",      "homepage": "/rest/api/content/58523691"    }  },  "history": {    "latest": true,    "createdBy": {      "type": "known",      "username": "semberson",      "userKey": "ff80808153317ba0015334545e58001f",      "profilePicture": {        "path": "/wiki/download/attachments/21463091/user-avatar",        "width": 48,        "height": 48,        "isDefault": false      },      "displayName": "Scott Emberson",      "_links": {        "self": "https://navmanwireless.atlassian.net/wiki/rest/experimental/user?key=ff80808153317ba0015334545e58001f"      },      "_expandable": {        "details": ""      }    },    "createdDate": "2016-08-25T06:59:35.586+01:00",    "_links": {      "self": "https://navmanwireless.atlassian.net/wiki/rest/api/content/58523699/history"    },    "_expandable": {      "lastUpdated": "",      "previousVersion": "",      "contributors": "",      "nextVersion": ""    }  },  "version": {    "by": {      "type": "known",      "username": "devops.tools",      "userKey": "ff80808156c0d30f0156d9cf5d2f0001",      "profilePicture": {        "path": "/wiki/download/attachments/59211847/user-avatar",        "width": 48,        "height": 48,        "isDefault": false      },      "displayName": "DevOps Tool",      "_links": {        "self": "https://navmanwireless.atlassian.net/wiki/rest/experimental/user?key=ff80808156c0d30f0156d9cf5d2f0001"      },      "_expandable": {        "details": ""      }    },    "when": "2016-08-30T09:46:43.457+01:00",    "friendlyWhen": "about 2 hours ago",    "message": "",    "number": 9,    "minorEdit": false,    "_links": {      "self": "https://navmanwireless.atlassian.net/wiki/rest/api/content/58523699/version/9"    },    "_expandable": {      "content": "/rest/api/content/58523699"    }  },  "extensions": {    "position": "none"  },  "_links": {    "webui": "/display/SysEnv/3+-+UAT+Environments",    "tinyui": "/x/MwB9Aw",    "collection": "/rest/api/content",    "base": "https://navmanwireless.atlassian.net/wiki",    "context": "/wiki",    "self": "https://navmanwireless.atlassian.net/wiki/rest/api/content/58523699"  },  "_expandable": {    "childTypes": "",    "container": "/rest/api/space/SysEnv",    "metadata": "",    "operations": "",    "children": "/rest/api/content/58523699/child",    "restrictions": "/rest/api/content/58523699/restriction/byOperation",    "ancestors": "",    "body": "",    "descendants": "/rest/api/content/58523699/descendant"  }}}

所以Number返回null,但我希望它返回的是9。

1 个答案:

答案 0 :(得分:0)

如果仔细观察你的json,你会发现它有一个具有属性编号的对象“version”。所以,你的代码就像这样

  var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                Console.Write(responseText);
                var data = (JObject)JsonConvert.DeserializeObject(responseText);
                var name = data["number"];
                Console.WriteLine("Version Number" + name);
            }