Skip to main content
 首页 » 编程设计

c#之C#中的JSON表格格式解析

2024年02月05日12kevingrace

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

{
    “ status”:“ REQUEST_SUCCEEDED”,
    “ responseTime”:66,
    “信息”: [],
    “结果”:{
        “系列”:[
            {
                “ seriesID”:“ CUUR0000SA0”,
                “数据”:[
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M10”,
                        “ periodName”:“十月”,
                        “ value”:“ 237.838”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M09”,
                        “ periodName”:“ 9月”,
                        “值”:“ 237.945”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M08”,
                        “ periodName”:“八月”,
                        “值”:“ 238.316”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M07”,
                        “ periodName”:“七月”,
                        “值”:“ 238.654”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M06”,
                        “ periodName”:“六月”,
                        “值”:“ 238.638”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M05”,
                        “ periodName”:“五月”,
                        “ value”:“ 237.805”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M04”,
                        “ periodName”:“ 4月”,
                        “值”:“ 236.599”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M03”,
                        “ periodName”:“三月”,
                        “ value”:“ 236.119”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M02”,
                        “ periodName”:“ 2月”,
                        “ value”:“ 234.722”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2015”,
                        “句号”:“ M01”,
                        “ periodName”:“一月”,
                        “ value”:“ 233.707”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M13”,
                        “ periodName”:“年度”,
                        “ value”:“ 236.736”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M12”,
                        “ periodName”:“十二月”,
                        “ value”:“ 234.812”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M11”,
                        “ periodName”:“ 11月”,
                        “ value”:“ 236.151”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M10”,
                        “ periodName”:“十月”,
                        “ value”:“ 237.433”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M09”,
                        “ periodName”:“ 9月”,
                        “ value”:“ 238.031”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M08”,
                        “ periodName”:“八月”,
                        “值”:“ 237.852”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M07”,
                        “ periodName”:“七月”,
                        “值”:“ 238.250”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M06”,
                        “ periodName”:“六月”,
                        “值”:“ 238.343”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M05”,
                        “ periodName”:“五月”,
                        “值”:“ 237.900”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M04”,
                        “ periodName”:“ 4月”,
                        “ value”:“ 237.072”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M03”,
                        “ periodName”:“三月”,
                        “ value”:“ 236.293”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M02”,
                        “ periodName”:“ 2月”,
                        “ value”:“ 234.781”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2014”,
                        “句号”:“ M01”,
                        “ periodName”:“一月”,
                        “值”:“ 233.916”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M13”,
                        “ periodName”:“年度”,
                        “值”:“ 232.957”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M12”,
                        “ periodName”:“十二月”,
                        “值”:“ 233.049”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M11”,
                        “ periodName”:“ 11月”,
                        “ value”:“ 233.069”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M10”,
                        “ periodName”:“十月”,
                        “ value”:“ 233.546”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M09”,
                        “ periodName”:“ 9月”,
                        “ value”:“ 234.149”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M08”,
                        “ periodName”:“八月”,
                        “值”:“ 233.877”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M07”,
                        “ periodName”:“七月”,
                        “ value”:“ 233.596”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M06”,
                        “ periodName”:“六月”,
                        “ value”:“ 233.504”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M05”,
                        “ periodName”:“五月”,
                        “ value”:“ 232.945”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M04”,
                        “ periodName”:“ 4月”,
                        “ value”:“ 232.531”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M03”,
                        “ periodName”:“三月”,
                        “ value”:“ 232.773”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M02”,
                        “ periodName”:“ 2月”,
                        “ value”:“ 232.166”,
                        “脚注”:[
                            {}
                        ]
                    },
                    {
                        “ year”:“ 2013”​​,
                        “句号”:“ M01”,
                        “ periodName”:“一月”,
                        “值”:“ 230.280”,
                        “脚注”:[
                            {}
                        ]
                    }
                ]
            }
        ]
    }
}

我的C#在下面...有趣的是,如果我使用RootObject类,则实际上返回状态和响应时间,但是如果我使用Datum类,则什么也没得到:

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(); 
 
 
        } 
 
    } 
} 


我希望能够列出所有Datum的year,periodName和值。有人可以帮我从JSON提取此数据吗?

请您参考如下方法:

您必须从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); 
}