我試圖從勞動和統計局CPI數據的工作,我有麻煩了反序列化下面的JSON數據:JSON表格格式解析在C#
{ 「狀態」:「REQUEST_SUCCEEDED」 「RESPONSETIME」:66, 「消息」:[], 「結果」:{ 「系列」:[ { 「seriesID」: 「CUUR0000SA0」, 「數據」:[ { 「年」 :「2015」, 「period」:「M10」, 「periodName」:「October」, 「值」: 「237.838」, 「腳註」:[ {} ] }, { 「年」: 「2015」, 「期間」: 「M09」, 「periodName」:「九月」, 「值」: 「237.945」, 「腳註」:[ {} ] }, { 「年」: 「2015」, 「期間」: 「M08」, 「periodName」: 「August」, 「value」:「238.316」, 「footnotes」:[ {} ] }, { 「年」: 「2015」, 「時期」: 「M07」, 「periodName」: 「七月」, 「值」: 「238.654」, 「腳註」 :[ {} ] }, { 「年」: 「2015」, 「時期」: 「M06」, 「periodName」: 「六一」, 「值」: 「238.638」, 「腳註「:[ {} ] }, { 「年」: 「2015」, 「期間」: 「M05」, 「periodName」: 「月」, 「值」: 「237.805」, 「腳註」:[ {} ] }, { 「年」: 「2015」, 「時期」: 「M04」, 「periodName」: 「四月」, 「值」: 「236.599」, 「腳註」: {} ] }, { 「year」:「2015」, 「期限」: 「M03」, 「periodName」: 「三月」, 「值」: 「236.119」, 「腳註」: {} ] }, { 「年」: 「2015」 , 「period」:「M02」, 「periodName」:「February」, 「value」:「234。722" , 「腳註」: {} ] }, { 「年」: 「2015」, 「時期」: 「M01」, 「periodName」: 「一月」, 「值」 「233.707」, 「腳註」:[ {} ] }, { 「年」: 「2014」, 「期間」: 「M13」, 「periodName」: 「年度」, 「值「:」236.736「, 」腳註「:[ {} ] }, { 「年」: 「2014」, 「期間」: 「M12」, 「periodName」: 「臘」, 「值」: 「234.812」, 「腳註」:[ {} ] }, { 「年」: 「2014」, 「時期」: 「M11」, 「periodName」: 「11月」, 「值」: 「236.151」, 「腳註」: [ {} ] }, { 「年」: 「2014」, 「時期」: 「M10」, 「periodName」: 「十月」, 「值」: 「237.433」, 「腳註」: {} ] }, { 「年」: 「2014」, 「期間」: 「M09」, 「periodName」: 「九」, 「值」: 「238.031」, 「腳註」:[ {} ] }, { 「year」:「2014」, 「period」:「M08」, 「periodName」: 「八月」, 「值」: 「237.852」, 「腳註」: {} ] }, { 「年」: 「2014」, 「時期」:「M07 」 「periodName」: 「七月」, 「值」: 「238.250」, 「腳註」: {} ] }, { 「年」: 「2014」, 「時期」: 「M06」, 「periodName」:「June」, 「value」:「238.343」, 「腳註」:[ {} ] }, { 「年」: 「2014」, 「期間」: 「M05」, 「periodName」: 「月」, 「值」:「237.900 」 「腳註」: {} ] }, { 「年」: 「2014」, 「時期」: 「M04」, 「periodName」: 「四月」, 「值」: 「237。072" , 「腳註」: {} ] }, { 「年」: 「2014」, 「時期」: 「M03」, 「periodName」: 「三月」, 「值」 「236.293」, 「腳註」: {} ] }, { 「年」: 「2014」, 「時期」: 「M02」, 「periodName」: 「二月」, 「值「:」234.781「, 」腳註「:[ {} ] }, { 「年」: 「2014」, 「時期」: 「M01」, 「periodName」: 「一月」, 「值」: 「233.916」, 「腳註」: {} ] }, { 「年」: 「2013」, 「時期」: 「M13」, 「periodName」: 「年」, 「值」: 「232.957」, 「腳註」: [ {} ] }, { 「年」: 「2013」, 「期間」: 「M12」, 「periodName」: 「臘」, 「值」: 「233.049」, 「腳註」:[ {} ] }, { 「年」: 「2013」, 「時期」: 「M11」, 「periodName」: 「11月」, 「值」: 「233.069」, 「腳註」: {} ] }, { 「year」:「2013」, 「period」:「M10」, 「periodName」: 「十月」, 「值」: 「233.546」, 「腳註」: {} ] }, { 「年」: 「2013」, 「時期」:「M09 」, 「periodName」: 「九」, 「值」: 「234.149」, 「腳註」:[ {} ] }, { 「年」: 「2013」, 「期間」: 「M08」, 「periodName」:「August」, 「value」:「233.877 」 「腳註」: {} ] }, { 「年」: 「2013」, 「時期」: 「M07」, 「periodName」: 「七月」, 「值」: 「233.596」, 「腳註」: {} ] }, { 「年」: 「2013」, 「時期」: 「M06」, 「periodName」: 「六一」, 「值「:」233。504" , 「腳註」:[ {} ] }, { 「年」: 「2013」, 「期間」: 「M05」, 「periodName」: 「月」, 「值」 「232.945」, 「腳註」: {} ] }, { 「年」: 「2013」, 「時期」: 「M04」, 「periodName」: 「四月」, 「值「:」232.531「, 」腳註「:[ {} ] }, { 「年」: 「2013」, 「時期」: 「M03」, 「periodName」: 「三月」, 「值」: 「232.773」, 「腳註」: {} ] }, { 「年」: 「2013」, 「時期」: 「M02」, 「periodName」: 「二月」, 「值」: 「232.166」, 「腳註」: [ {} ] }, { 「年」: 「2013」, 「時期」: 「M01」, 「periodName」: 「一月」, 「值」: 「230.280」, 「腳註」: {} ] } ] } ] }}
我的C#是下面......有趣的是,如果我使用RootObject類其實我返回狀態和響應時間,但如果我用基準類,我得到什麼:
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和values。任何人都可以幫助我從這個JSON中提取這些數據嗎?
謝謝你這麼多的清除此了! – user3486773