简体   繁体   English

从 C# 中的 JSON 检索 Mixpanel 数据

[英]Retrieving Mixpanel data from JSON in C#

I am trying to get data from a Mixpanel API.我正在尝试从 Mixpanel API 获取数据。 The JSON result looks like this: JSON 结果如下所示:

{"computed_at": "2019-11-19T10:36:33.908802+00:00", "legend_size": 1, "data": {"series": ["2019-11-11", "2019-11-12", "2019-11-13"], "values": {"page views": {"2019-11-12": 111, "2019-11-11": 573, "2019-11-13": 209}}}}

I am struggling with nested JSON (despite looking at a fair few StackOverflow articles on this) and I want to try and get the values 111 , 573 and 209 for the page views.我正在为嵌套的 JSON 苦苦挣扎(尽管查看了一些关于此的 StackOverflow 文章),我想尝试获取页面视图的值111573209 How do I access these values?如何访问这些值?

I've tried the following:我尝试了以下方法:

var result = {"computed_at": "2019-11-19T10:36:33.908802+00:00", "legend_size": 1, "data": {"series": ["2019-11-11", "2019-11-12", "2019-11-13"], "values": {"page views": {"2019-11-12": 111, "2019-11-11": 573, "2019-11-13": 209}}}}
var rawData = result["data"]["values"]["page views"][0] 

but this doesn't work and I can't figure out how to access the individual values.但这不起作用,我不知道如何访问各个值。 Can anyone help please?有人可以帮忙吗?

EDIT:编辑:

When using the json to C# converter (as suggested in the comments to this post) the outcome is as follows:当使用 json 到 C# 转换器时(如本文评论中所建议),结果如下:

public class PageViews
{
    public int __invalid_name__2019-11-20 { get; set; }
    public int __invalid_name__2019-11-19 { get; set; }
}

public class Values
{
    public PageViews __invalid_name__page views { get; set; }
}

public class Data
{
    public List<string> series { get; set; }
    public Values values { get; set; }
}

public class RootObject
{
    public DateTime computed_at { get; set; }
    public int legend_size { get; set; }
    public Data data { get; set; }
}

and I am still unsure how to get the numbers from this?我仍然不确定如何从中获得数字?

There are two approaches you could take.您可以采取两种方法。 First, I'd deserialize the json string using this library - Newtonsoft .首先,我将使用这个库 - Newtonsoft反序列化 json 字符串。

You can then go through the json using JObjects, or you can create a C#-Class that represents your json data and then deserialize it into a corresponding object. You can then go through the json using JObjects, or you can create a C#-Class that represents your json data and then deserialize it into a corresponding object.

  1. Via a class:通过 class:

To create the json class there is this website, which creates the classes (also nested) just as the json needs them to be: http://json2csharp.com/ To create the json class there is this website, which creates the classes (also nested) just as the json needs them to be: http://json2csharp.com/

Then you use:然后你使用:

var myObject = JsonConvert.DeserializeObject<MyClass>(jsonString);

and then simply go through your object properties.然后通过您的 object 属性简单地 go 。

  1. Via JObject通过 JObject

JObjects allow you to search through the json string easily as such: JObjects 允许您轻松搜索 json 字符串,如下所示:

JObject myObject = JObject.Parse(json);

var results = myObject["data"]["values"]["page views"].First();

Edit: Since your Json contains keys, which begin with a number (and are not valid c# variable names) its a bit more tricky but doable.编辑:由于您的 Json 包含以数字开头的键(并且不是有效的 c# 变量名称),因此它有点棘手但可行。 The automatic converter failed on this.自动转换器在此失败。

  string json = "{\"computed_at\": \"2019 - 11 - 19T10: 36:33.908802 + 00:00\", \"legend_size\": 1, \"data\": {\"series\": [\"2019 - 11 - 11\", \"2019 - 11 - 12\", \"2019 - 11 - 13\"], \"values\": {\"page views\": {\"2019 - 11 - 12\": 111, \"2019 - 11 - 11\": 573, \"2019 - 11 - 13\": 209}}}}";

  var myObject = JsonConvert.DeserializeObject<RootObject>(json);

  foreach (var view in myObject.data.series)
  {
     int number = myObject.data.values.page_views[view];
     Console.WriteLine($"{number} views on {view}");
  }

With the RootObject class being: RootObject class 为:

 public class Values
{
    [JsonProperty("page views")]
    public Dictionary<string, int> page_views { get; set; }
}

public class Data
{
    public List<string> series { get; set; }
    public Values values { get; set; }
}

public class RootObject
{
    public string computed_at { get; set; }
    public int legend_size { get; set; }
    public Data data { get; set; }
}

The trick was to use a Dictionary<string, int> type for the date-values, and rename the property to a valid c# name.诀窍是对日期值使用Dictionary<string, int>类型,并将属性重命名为有效的 c# 名称。 The JsonDeserializer still finds the data, because we told it to look for "page views" using the JsonProperty attribute. JsonDeserializer 仍然可以找到数据,因为我们告诉它使用JsonProperty属性查找“页面浏览量”。 This way the class is also flexible for multiple entries in your "page views" field.这样,class 也可以灵活地用于“页面浏览”字段中的多个条目。

Hope this helps希望这可以帮助

You were close.你很亲密。 In the line在行

var rawData = result["data"]["values"]["page views"][0]

you are attempting to access index 0 of an object.您正在尝试访问 object 的索引 0。 This doesn't really make sense because objects are basically dictionaries or hash tables, which means they are unordered and must be accessed by a key rather than an index (See Retrieving a property of a JSON object by index? ).这实际上没有任何意义,因为对象基本上是字典或 hash 表,这意味着它们是无序的,必须通过键而不是索引来访问(请参阅检索 JSON ZA8CFDE63311BD59EB2AC96F8 的属性)。

What you want looks like你想要的样子

var rawData = result["data"]["values"]["page views"]['2019-11-12'] to access the number of page views (which is 111 ) for the date 2019-11-12 . var rawData = result["data"]["values"]["page views"]['2019-11-12']访问2019-11-12日期的页面浏览量(即111 )。

You can then loop through the object and get each number of page views as follows:然后您可以遍历 object 并获取每个页面浏览量,如下所示:

var pageViews = [];
for (var key of Object.keys(result.data.values['page views'])) {
    pageViews.push(result.data.values['page views'][key]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM