locked
JSON.NET RRS feed

  • Question

  • User1693790852 posted

    Trying to parse json string using JSON.NET.  I am able to convert json string to jobject

    Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(jsonResponse.Trim()); But don't know how to parse with it. I tried

    string name = (string)o["measures"] ; but it does not work.  How can I parse these values? For examples Visitors.


    "data": {
        "09/14/2010-09/14/2010": {
          "Attributes": null,
          "measures": {
            "Page Views": 59453.0,
            "Visits": 4800.0,
            "Visitors": 4196.0,
            "Bounce Rate": 35.65,
            "Avg. Time on Site": 612.99,
            "Avg. Visitors per Day": 4196.0,
            "Page Views per Visit": 12.39,
            "New Visitors": 3538.0
          },
          "SubRows": {
            "09/14/2010": {
              "Attributes": null,
              "measures": {
                "Page Views": 59453.0,
                "Visits": 4800.0,
                "Visitors": 4196.0,
                "Bounce Rate": 35.65,
                "Avg. Time on Site": 612.99,
                "Avg. Visitors per Day": 4196.0,
                "Page Views per Visit": 12.39,
                "New Visitors": 3538.0
              },
              "SubRows": null
            }

    Tuesday, September 14, 2010 12:36 PM

All replies

  • User1520641890 posted

    you almost have it, try this:

    <%@ WebHandler Language="C#" Class="testJson" %>
    using System;
    using System.Web;
    using Newtonsoft.Json.Linq;
    
    public class testJson : IHttpHandler {
      public void ProcessRequest (HttpContext context) {
        string text = @"{
      ""data"": {
        ""09/14/2010-09/14/2010"": {
          ""Attributes"": null,
          ""measures"": {
            ""Page Views"": 59453.0,
            ""Visits"": 4800.0,
            ""Visitors"": 4196.0,
            ""Bounce Rate"": 35.65,
            ""Avg. Time on Site"": 612.99,
            ""Avg. Visitors per Day"": 4196.0,
            ""Page Views per Visit"": 12.39,
            ""New Visitors"": 3538.0
          }
        }
      }
    }";
        JObject jo = JObject.Parse(text);
        context.Response.Write((string)jo
          ["data"]["09/14/2010-09/14/2010"]["measures"]["Visitors"]
          .ToString()
        );
      }
      public bool IsReusable {
        get { return false; }
      }
    }


    Thursday, September 16, 2010 12:01 AM
  • User1693790852 posted

    Thank you it worked :)


    Thursday, September 16, 2010 8:57 AM