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

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

问题描述

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

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"
    }
  }
}

请注意,"Time Series"属性以1min,5min,15min,30min,60min的间隔出现,即"Time Series (##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 Series (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天全站免登陆