JSON c#中的表格格式解析

JSON tabular format parsing in c#

我正在尝试使用来自劳工统计局的 CPI 数据,但在反序列化以下 JSON 数据时遇到问题:

{ "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# 在下面...有趣的是,如果我使用 RootObject class 我实际上 return 状态和响应时间但是如果我使用 Datum class我什么也没得到:

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
{

public class Footnote
{
}

public class Datum
{
    public string year { get; set; }
    public string period { get; set; }
    public string periodName { get; set; }
    public string value { get; set; }
    public List<Footnote> footnotes { get; set; }

    public override string ToString()
    {
        return string.Format("Year: {0}", year);
    }
}

public class Series
{
    public string seriesID { get; set; }
    public List<Datum> data { get; set; }

    public override string ToString()
    {
        return string.Format("Series: {0} \n Data: {1}", seriesID, data);
    }
}

public class Results
{
    public List<Series> series { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public int responseTime { get; set; }
    public List<object> message { get; set; }
    public Results Results { get; set; }

    public override string ToString()
    {
        return string.Format("Status: {0} \n Response 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\Desktop\json.txt");


        ////This returns nothing
        Datum p1 = JsonConvert.DeserializeObject<Datum>(jsonString);


        ////This works and returns the data
        //RootObject p1 = JsonConvert.DeserializeObject<RootObject>(jsonString);



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


        }

    }
}

我希望能够列出所有基准面的年份、期间名称和值。谁能帮我从 JSON 中提取这些数据?

如果你有一个像你发布的那样的完整 json,你不能直接从它反序列化你的数据,你需要解析整个结构,所以这就是为什么

RootObject p1 = JsonConvert.DeserializeObject<RootObject>(jsonString);

作品和returns数据

此时您只需导航 RootObject 即可找到您需要的数据

Datum 不是单个数据,而是嵌套在对象层次结构中,因此您需要从正确的位置获取它

你的RootObject有一个Series列表(series属性),每个Series都有一个Datum列表(data属性),所以你必须做一个聚合函数或者得到一个特定的系列,然后检查它的基准列表

给你 json,这应该足够了(未经测试)

RootObject p1 = JsonConvert.DeserializeObject<RootObject>(jsonString);
var datas = p1.Result.series[0].data;

更新: 我为您创建了一个 fiddle 来向您展示读取数据的完整过程: https://dotnetfiddle.net/dxel3K

例如在控制台中转储你的数据这就是你需要的

RootObject p1 = JsonConvert.DeserializeObject<RootObject>(jsonString);

    foreach(var data in p1.Results.series[0].data){
        Console.WriteLine(String.Format("year: {0}; period: {1}; periodName: {2}", data.year,data.period,data.periodName));
    }

您必须从 RootObject 开始,因为 json 就是这样格式化的。然后您可以导航到基准数组。

RootObject p1 = JsonConvert.DeserializeObject<RootObject>(jsonString);
Console.WriteLine(p1.Results.series[0].data[0].year);

要列出所有值,可以循环:

foreach(Datum d in p1.Results.series[0].data)
{
      Console.WriteLine(d.year + " : " + d.period + " : " + d.periodName + " : " + d.value);
} 

我相信你的信息就在那里你只需要去挖掘一点。在您的根对象 class 中,您可以像这样访问您的基准信息,

RootObject p1 = JsonConvert.DeserializeObject<RootObject>(jsonString);
// Keep in mind this will only iterate over first Series, if you have multiple
// series you would need to do a nested loop over that collection.
foreach(Datum item in p1.Results.series.First().data)
{
     //display Item info.
}

使用 Javascript Serializer :

var parser = new JavaScriptSerializer();
var data = parser.Deserialize<dynamic>(missionGeoData);

foreach (var temp in data){       
     yourList.Add(temp["fieldName"].ToString());
}