从JObject获取数据

时间:2018-08-12 19:18:58

标签: c# asp.net json.net

我已经将JSON数据存储在字符串中,并且通过使用JObject,我试图从JSON数据中获取值。我只是无法弄清楚代码的根本问题是什么,因为我无法从JSON对象获取数据。我的代码片段附在下面。如果有人可以帮助我解决问题,将不胜感激。

String text;
try {
    var response = (HttpWebResponse)request.GetResponse();
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        text = sr.ReadToEnd();
        JObject jObject = JObject.Parse(text);

        string haha = (string)jObject["value/segments/requests/count/sum"];
        ViewBag.gotstring = haha;
    }

    System.Diagnostics.Debug.WriteLine(text);
} catch(Exception e) {
    System.Diagnostics.Debug.WriteLine(url);
    System.Diagnostics.Debug.WriteLine(e.ToString()); }

return View();

这是JSON:

{
  "value": {
    "start": "2018-08-12T04:44:38.941Z",
    "end": "2018-08-12T16:44:38.941Z",
    "interval": "PT30M",
    "segments": [
      {
        "start": "2018-08-12T14:00:00Z",
        "end": "2018-08-12T14:30:00Z",
        "segments": [
          {
            "requests/count": {
              "sum": 2
            },
            "request/name": "GET Home/Index"
          },
          {
            "requests/count": {
              "sum": 1
            },
            "request/name": "GET Home/About"
          },
          {
            "requests/count": {
              "sum": 1
            },
            "request/name": "GET Home/Contact"
          }
        ]
      },
      {
        "start": "2018-08-12T14:30:00Z",
        "end": "2018-08-12T15:00:00Z",
        "segments": [
          {
            "requests/count": {
              "sum": 2
            },
            "request/name": "GET Account/Register"
          },
          {
            "requests/count": {
              "sum": 1
            },
            "request/name": "GET Account/Login"
          }
        ]
      },
      {
        "start": "2018-08-12T15:30:00Z",
        "end": "2018-08-12T16:00:00Z",
        "segments": [
          {
            "requests/count": {
              "sum": 8
            },
            "request/name": "GET Home/Index"
          },
          {
            "requests/count": {
              "sum": 8
            },
            "request/name": "GET Home/About"
          }
        ]
      }
    ]
  }
}

2 个答案:

答案 0 :(得分:1)

jObject不能这样工作。它返回可以按键查询的字典,但是键是单级的。即您将能够获得如下数据:

var haha = jObject["value"]["segments"];

但是除此之外,它变得非常复杂。定义一个代表您的JSON并将其序列化的C#类会更好。在Visual Studio中,一个简单的`Edit => Paste Special => JSON as Class”提供了这样的功能:

    public class Rootobject
    {
        public Value value { get; set; }
    }

    public class Value
    {
        public DateTime start { get; set; }
        public DateTime end { get; set; }
        public string interval { get; set; }
        public Segment[] segments { get; set; }
    }

    public class Segment
    {
        public DateTime start { get; set; }
        public DateTime end { get; set; }
        public Segment1[] segments { get; set; }
    }

    public class Segment1
    {
       [JsonProperty("requests/count")]
       public RequestsCount requestscount { get; set; }
       [JsonProperty("request/name")]
       public string requestname { get; set; }
    }

    public class RequestsCount
    {
        public int sum { get; set; }
    }

然后像这样反序列化:

var serialised = JsonConvert.DeserializeObject<Rootobject>(json);

var haha = serialised.value.segments.FirstOrDefault().segments.FirstOrDefault().requestscount.sum;

这是一个有效的示例:https://dotnetfiddle.net/CZgMNE

答案 1 :(得分:0)

您可以尝试:

编辑::段似乎是一个数组,这只会为您获得第一个段的总和

string haha = (string)jObject["value"]["segments"][0]["segments"]["requests/count"]["sum"];