【问题标题】:Using c# to Parse JSON data from Bureau of labor and statistics使用 c# 解析来自劳动统计局的 JSON 数据
【发布时间】:2016-03-12 21:10:45
【问题描述】:

我正在使用 c# 来解析来自劳工统计局的 JSON 数据,但我遇到了困难。最终我想将此数据插入到 SQL 表中。我可以访问 JSON 数据的某些部分,但是在尝试访问数组内的对象时出现错误。在我的代码中注释掉了指向数据的链接。一天只能查询25次...

这是来自 BLS 链接的 JSON 数据(我正在使用具有相同数据的 txt 文件):

{
    "status": "REQUEST_SUCCEEDED",
    "responseTime": 66,
    "message": [],
    "Results": {
        "series": [
            {
                "seriesID": "CUUR0000SA0",
                "data": [
                    {
                        "year": "2015",
                        "period": "M10",
                        "periodName": "October",
                        "value": "237.838",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M09",
                        "periodName": "September",
                        "value": "237.945",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M08",
                        "periodName": "August",
                        "value": "238.316",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M07",
                        "periodName": "July",
                        "value": "238.654",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M06",
                        "periodName": "June",
                        "value": "238.638",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M05",
                        "periodName": "May",
                        "value": "237.805",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M04",
                        "periodName": "April",
                        "value": "236.599",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M03",
                        "periodName": "March",
                        "value": "236.119",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M02",
                        "periodName": "February",
                        "value": "234.722",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2015",
                        "period": "M01",
                        "periodName": "January",
                        "value": "233.707",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M13",
                        "periodName": "Annual",
                        "value": "236.736",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M12",
                        "periodName": "December",
                        "value": "234.812",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M11",
                        "periodName": "November",
                        "value": "236.151",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M10",
                        "periodName": "October",
                        "value": "237.433",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M09",
                        "periodName": "September",
                        "value": "238.031",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M08",
                        "periodName": "August",
                        "value": "237.852",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M07",
                        "periodName": "July",
                        "value": "238.250",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M06",
                        "periodName": "June",
                        "value": "238.343",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M05",
                        "periodName": "May",
                        "value": "237.900",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M04",
                        "periodName": "April",
                        "value": "237.072",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M03",
                        "periodName": "March",
                        "value": "236.293",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M02",
                        "periodName": "February",
                        "value": "234.781",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2014",
                        "period": "M01",
                        "periodName": "January",
                        "value": "233.916",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M13",
                        "periodName": "Annual",
                        "value": "232.957",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M12",
                        "periodName": "December",
                        "value": "233.049",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M11",
                        "periodName": "November",
                        "value": "233.069",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M10",
                        "periodName": "October",
                        "value": "233.546",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M09",
                        "periodName": "September",
                        "value": "234.149",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M08",
                        "periodName": "August",
                        "value": "233.877",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M07",
                        "periodName": "July",
                        "value": "233.596",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M06",
                        "periodName": "June",
                        "value": "233.504",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M05",
                        "periodName": "May",
                        "value": "232.945",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M04",
                        "periodName": "April",
                        "value": "232.531",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M03",
                        "periodName": "March",
                        "value": "232.773",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M02",
                        "periodName": "February",
                        "value": "232.166",
                        "footnotes": [
                            {}
                        ]
                    },
                    {
                        "year": "2013",
                        "period": "M01",
                        "periodName": "January",
                        "value": "230.280",
                        "footnotes": [
                            {}
                        ]
                    }
                ]
            }
        ]
    }
}

我有兴趣尝试从中获取年、月和值数据,但我只能使用这个 c# 访问第一部分。我不确定如何获取其余的 JSON 对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Net;
using System.Data.SqlClient;
using System.Web;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.ComponentModel;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.IO;


namespace ConsoleApplication5
{

class Person
{


    public string status { get; set; }

    public string responseTime { get; set; }


    public override string ToString()
    {
        return string.Format("Status: {0}  \nResponse Time: {1}", status, responseTime);
    }


}

class Program
{

    static void Main(string[] args)
    {
        TryParse();
    }


    static void getJson()
    {

    }

    static void TryParse()
    {
        //WebRequest request = WebRequest.Create("http://api.bls.gov/publicAPI/v1/timeseries/data/CUUR0000SA0");
        //WebResponse response = request.GetResponse();

        //string jsonString = response.ToString();

        string jsonString = File.ReadAllText(@"C:\Users\h\Desktop\json.txt");

        JavaScriptSerializer ser = new JavaScriptSerializer();
        Person p1 = ser.Deserialize<Person>(jsonString);

        Console.WriteLine(p1);
        Console.ReadLine();

    }

   }
}

有人可以帮我访问年份、时期名称和价值数据吗?如果是这样,我可能会从那里将它塑造成一个插入语句。

【问题讨论】:

    标签: c# json parsing arraylist


    【解决方案1】:

    我推荐使用 Xamasoft JSON 类生成器,您可以使用浏览器下载 json 数据的副本,然后将其放入该工具中。它将为您提供或至少帮助您构建反序列化数据所需的类文件。另请注意,我发现在使用这些 json 类时,如果您使用 JSON.NET,您将使用数组 [],但如果您使用 RestSharp,则需要将它们更改为 List。

    【讨论】:

      【解决方案2】:

      你可能需要用这种类架构反序列化

      public class Rootobject
      {
          public string status { get; set; }
          public int responseTime{ get; set; }
          public object[] message{ get; set; }
          public Results Results{ get; set; }
      }
      public class Results
      {
          public Series[] series{ get; set; }
      }
      public class Series
      {
          public string seriesID{ get; set; }
          public Datum[] data{ get; set; }
      }
      public class Datum
      {
          public string year{ get; set; }
          public string period{ get; set; }
          public string periodName{ get; set; }
          public string value{ get; set; }
          public Footnote[] footnotes{ get; set; }
      }
      public class Footnote
      {
      }
      

      然后你可以写

      Deserialize<Rootobject>(jsonString);
      

      【讨论】:

      • 这是从 JSON 到 c# 的吗?因为我在那里并且很早就得到了这个,当时这些都没有意义,但是你把代码放到上下文中的方式扩大了我的想法!这可能是我在这里看到的这个问题的最明确的答案。尽快标记为答案。
      • 乐于助人。 :) 不,这不是来自JSON2C# 我只是关注你的评论。 :D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      相关资源
      最近更新 更多