用 C# 解析复杂的 JSON [英] Parsing Complex JSON with C#

查看:93
本文介绍了用 C# 解析复杂的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JSON 的新手,我有一些 JSON 试图用 C# 解析.

I am a newbie to JSON and I have some JSON that I trying to parse with C#.

我曾尝试创建一个用数据表示的类,但我的属性名称是基于时间的,因此我必须对我的数据契约进行硬编码.我尝试过 JSON.NET 和 LINQ 对数据进行排序,但由于奇怪的对象/属性而不断得到空值.

I have tried making a class to represent by data, but the names of my properties are based on times so I’d have to hard code my data contracts. I’ve tried JSON.NET and LINQ to sort through the data, but keep getting null values due to the strange object/property.

我对 JSON 还是很陌生,所以我确定有一个简单的解决方法,我只是不确定如何正确提问.感谢您的帮助.

Again I am very new to JSON so I’m sure there is a simple fix to this, I’m just not sure how to ask the question correctly. Thank you for your help.

下面是我正在努力解析的一小部分 JSON 示例.再次感谢.

Below is a small sample of JSON I am struggling to parse. Again thanks.

      {
  "Meta Data": {
    "1. Information": "Intraday (1min) prices and volumes",
    "2. Symbol": "MU",
    "3. Last Refreshed": "2017-05-30 16:00:00",
    "4. Interval": "1min",
    "5. Output Size": "Full size",
    "6. Time Zone": "US/Eastern"
  },
  "Time Series (1min)": {
    "2017-05-30 16:00:00": {
      "1. open": "30.7200",
      "2. high": "30.7300",
      "3. low": "30.7000",
      "4. close": "30.7000",
      "5. volume": "1390302"
    },
    "2017-05-30 15:59:00": {
      "1. open": "30.7750",
      "2. high": "30.7800",
      "3. low": "30.7200",
      "4. close": "30.7250",
      "5. volume": "380134"
    }
  }
}

请注意,时间序列" 属性以 1 分钟、5 分钟、15 分钟、30 分钟、60 分钟为间隔,即 时间序列 (##min)" 用于各种##min.

Note that the "Time Series" properties come in 1min, 5min, 15min, 30min, 60min intervals, i.e. "Time Series (##min)" for various ##min.

推荐答案

您可以使用这些类反序列化那个特定的 Json 文件,这里我假设 Time 中的两个对象系列 (1min) 在每个 json 文件中将具有相同的名称.但考虑到它们是日期,我很确定每次下载 json 时都会有所不同.

You can use this classes to deserialize that particular Json file, here I'm assuming that the two objects inside Time Series (1min) will be have the same names in every json file. But considering that they are dates, I'm pretty sure the will be different each time you will download the json.

只是为了让您了解您可以使用 Newtonsoft Json 属性做什么:

Just to give you a little idea of what you can do with Newtonsoft Json attributes:

public class MetaData
{
    [JsonProperty("1. Information")]
    public string Information { get; set; }

    [JsonProperty("2. Symbol")]
    public string Symbol { get; set; }

    [JsonProperty("3. Last Refreshed")]
    public string LastRefreshed { get; set; }

    [JsonProperty("4. Interval")]
    public string Interval { get; set; }

    [JsonProperty("5. Output Size")]
    public string OutputSize { get; set; }

    [JsonProperty("6. Time Zone")]
    public string TimeZone { get; set; }
}

public class T1
{
    [JsonProperty("1. Information")]
    public string Open { get; set; }

    [JsonProperty("2. high")]
    public string High { get; set; }

    [JsonProperty("3. low")]
    public string Low { get; set; }

    [JsonProperty("4. close")]
    public string Close { get; set; }

    [JsonProperty("5. volume")]
    public string Volume { get; set; }
}

public class T2
{
    [JsonProperty("1. Information")]
    public string Open { get; set; }

    [JsonProperty("2. high")]
    public string High { get; set; }

    [JsonProperty("3. low")]
    public string Low { get; set; }

    [JsonProperty("4. close")]
    public string Close { get; set; }

    [JsonProperty("5. volume")]
    public string Volume { get; set; }
}

public class TimeSeries
{
    [JsonProperty("2017-05-30 16:00:00")]
    public T1 T1 { get; set; }

    [JsonProperty("2017-05-30 15:59:00")]
    public T2 T2 { get; set; }
}

public class RootObject
{
    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }

    [JsonProperty("Time Series (1min)")]
    public TimeSeries TimeSeries { get; set; }
}

然后,当你反序列化时:

Then, when you deserialize:

var deserializedObject = JsonConvert.DeserializeObject<RootObject>(
    File.ReadAllText("exampleFile.json"));

如果你能告诉我们更多关于你的 json 文件的信息,我们可以更好地帮助你.

If you can tell us something more about you json file, we could help you better.

这篇关于用 C# 解析复杂的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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