2015-12-08 62 views
1

我試圖從勞動和統計局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中提取這些數據嗎?

回答

2

你必須從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); 
} 
+0

謝謝你這麼多的清除此了! – user3486773

1

,如果你有一個完整的JSON像您發佈的一個,你不能從它反序列化直你的數據,你需要分析整個結構,所以這就是爲什麼

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

作品和返回數據

在這一點上你剛纔導航RootObject找到你需要

數據和基準是不是一個單一的數據,但它嵌套到對象hierarcy,所以你需要從正確的地方

得到它

你RootObject有系列(系列屬性)的列表,每個系列都有數據(data屬性)的列表,所以你必須做一個聚合函數或獲取特定的系列,然後檢查它的基準列表

給你JSON,這應該是足夠的(未經測試)

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

更新: 我爲您創建了一個小提琴,顯示你的全部進程讀取您的數據: https://dotnetfiddle.net/dxel3K

例如在控制檯中dump你的數據這就是你需要的東西

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

我相信你的信息是存在的,你只需要去挖掘一點點。在你的根對象類,你可以訪問您的基準信息像這樣,

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. 
} 
+0

非常好的信息。我希望我可以選擇多個答案 – user3486773

0

使用Javascript Serializer

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

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