2015-11-30 137 views
0

我有以下有效的JSON,我需要遍歷結果。我正在使用JSON.NET和C#。我能夠獲得SUCCESS的價值,但我不知道如何獲得任何指導都會有所幫助。使用JSON.NET循環瀏覽JSON結果

{ 
    "SUCCESS": 1, 
    "ERRMSG": "", 
    "COLUMNSANDDATA": { 
     "COLUMNS": ["LASTNAME", "FIRSTNAME", "EMAILADDRESS", "COURSENAME", "PROGRAMID", 
        "ENROLLMENTSTARTDATE", "COMPLETIONDATE", "GRADE", "SCORE", 
        "PASSED_NOTPASSED", "TYPEOFCREDITS", "CREDITSEARNED", "INSTRUCTORNAME", 
        "INSTRUCTOREMAILADDRESS", "CLIENTNAME", "COMMUNITYNAME", 
        "CERTIFICATESENTDATE", "DURATIONTYPE", "DURATIONMINUTES", 
        "LOGIN"], 
     "DATA": [ 
      ["Beane", "Coffee", "[email protected]", "Program with One Essay Test", null, 
      "January, 06 2014 18:06:56", "January, 06 2014 18:57:53", "Incomplete", null, 
      "Not Passed", "Musical Note", 0.00, "Ray Bradbury", "[email protected]", 
      "Hogarth's Flying Circus", "Captain's Club", null, null, null, 
      "[email protected]"], 
      ["Beane", "Navy", "[email protected]", "Program with One Essay Test", null, 
      "January, 06 2014 18:06:56", "January, 06 2014 18:36:39", "Pass", 95.00, 
      "Passed", "Musical Note", 1.00, "Ray Bradbury", "[email protected]", 
      "Hogarth's Flying Circus", "Captain's Club", "January, 06 2014 08:00:00", 
      null, null, "NavyB"] 
     ] 
    } 
} 

我能夠使用這個代碼塊

using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
{ 
    var result = streamReader.ReadToEnd(); 

    var deserializer = new JavaScriptSerializer(); 
    var jsonObj = (IDictionary<string, object>)deserializer.DeserializeObject(result); ; 

    Response.Write((string)jsonObj["SUCCESS"]); 
} 
+0

你確定你使用[Json.Net](http://www.newtonsoft.com/json)? ['JavaScriptSerializer'](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v = vs.110).aspx)是Microsoft提供的類(不是Json.Net)。 –

+0

是的。我安裝了JSON.NET,因爲我看過的很多示例都使用它。該項目最初是使用MS提供的類庫創建的。 –

回答

1

像這樣的東西應該努力獲得成功值,對不起,這不是測試。

JArray data_list = (JArray)jsonObj["COLUMNSANDDATA"]["DATA"]; 

foreach (JObject data in data_list) { 
    string col_0 = (string)data[0]; 
} 
+0

「不能用[]對'object'類型的表達式應用索引,我看到你正在嘗試做什麼,因爲我只需要一個或兩個值,這就是我要追求的路線。 –

0
  1. Cenerate class
  2. 反序列化的實例該類

public class COLUMNSANDDATA 
{ 
    public List<string> COLUMNS { get; set; } 
    public List<List<object>> DATA { get; set; } 
} 

public class RootObject 
{ 
    public int SUCCESS { get; set; } 
    public string ERRMSG { get; set; } 
    public COLUMNSANDDATA COLUMNSANDDATA { get; set; } 
} 


var deserializer = new JavaScriptSerializer(); 
var jsonObj = deserializer.DeserializeObject<RootObject>(result); 

foreach(col in jsonObj.COLUMNSANDDATA.COLUMNS) 
{ 
    //... 
} 
+0

這真的是必要的嗎?如果用戶只想要,例如,每個項目的單個屬性?想象一下,如果JSON更大...... – Umair

+0

@Umair它取決於我不知道所有的要求,所以,我只展示一種方式,它是如何可以解決的問題,在許多情況下,我的答案很糟糕,但是在很多情況下,這是很好的。在這裏,我認爲,這已經夠好了 – Backs

0

我強烈建議您使用自動生成的類最舒適的瀏覽和使用theese類。

看看這篇文章How to auto-generate a C# class file from a JSON object string它會幫助你通過自動生成classe表單json和xml。順便說一句,它將在未來幫助你。

var deserializer = new JavaScriptSerializer(); var jsonObj = deserializer.DeserializeObject<RootObject>(result);

0

你可以通過JSON循環,傾倒東西展現出來是這樣的:

string json = @" 
{ 
    ""SUCCESS"": 1, 
    ""ERRMSG"": """", 
    ""COLUMNSANDDATA"": { 
     ""COLUMNS"": [""LASTNAME"", ""FIRSTNAME"", ""EMAILADDRESS"", ""COURSENAME"", ""PROGRAMID"", ""ENROLLMENTSTARTDATE"", ""COMPLETIONDATE"", ""GRADE"", ""SCORE"", ""PASSED_NOTPASSED"", ""TYPEOFCREDITS"", ""CREDITSEARNED"", ""INSTRUCTORNAME"", ""INSTRUCTOREMAILADDRESS"", ""CLIENTNAME"", ""COMMUNITYNAME"", ""CERTIFICATESENTDATE"", ""DURATIONTYPE"", ""DURATIONMINUTES"", ""LOGIN""], 
     ""DATA"": [ 
      [""Beane"", ""Coffee"", ""[email protected]"", ""Program with One Essay Test"", null, ""January, 06 2014 18:06:56"", ""January, 06 2014 18:57:53"", ""Incomplete"", null, ""Not Passed"", ""Musical Note"", 0.00, ""Ray Bradbury"", ""[email protected]"", ""Hogarth's Flying Circus"", ""Captain's Club"", null, null, null, ""[email protected]""], 
      [""Beane"", ""Navy"", ""[email protected]"", ""Program with One Essay Test"", null, ""January, 06 2014 18:06:56"", ""January, 06 2014 18:36:39"", ""Pass"", 95.00, ""Passed"", ""Musical Note"", 1.00, ""Ray Bradbury"", ""[email protected]"", ""Hogarth's Flying Circus"", ""Captain's Club"", ""January, 06 2014 08:00:00"", null, null, ""NavyB""] 
     ] 
    } 
}"; 

JObject root = JObject.Parse(json); 
JObject colsAndData = (JObject)root["COLUMNSANDDATA"]; 
JArray cols = (JArray)colsAndData["COLUMNS"]; 
foreach (JArray row in colsAndData["DATA"]) 
{ 
    for (int i = 0; i < row.Count; i++) 
    { 
     string colName = (string)cols[i]; 
     string value = (string)row[i]; 
     Console.WriteLine(colName + ": " + value); 
    } 
    Console.WriteLine(); 
} 

輸出:

LASTNAME: Beane 
FIRSTNAME: Coffee 
EMAILADDRESS: [email protected] 
COURSENAME: Program with One Essay Test 
PROGRAMID: 
ENROLLMENTSTARTDATE: January, 06 2014 18:06:56 
COMPLETIONDATE: January, 06 2014 18:57:53 
GRADE: Incomplete 
SCORE: 
PASSED_NOTPASSED: Not Passed 
TYPEOFCREDITS: Musical Note 
CREDITSEARNED: 0 
INSTRUCTORNAME: Ray Bradbury 
INSTRUCTOREMAILADDRESS: [email protected] 
CLIENTNAME: Hogarth's Flying Circus 
COMMUNITYNAME: Captain's Club 
CERTIFICATESENTDATE: 
DURATIONTYPE: 
DURATIONMINUTES: 
LOGIN: [email protected] 

LASTNAME: Beane 
FIRSTNAME: Navy 
EMAILADDRESS: [email protected] 
COURSENAME: Program with One Essay Test 
PROGRAMID: 
ENROLLMENTSTARTDATE: January, 06 2014 18:06:56 
COMPLETIONDATE: January, 06 2014 18:36:39 
GRADE: Pass 
SCORE: 95 
PASSED_NOTPASSED: Passed 
TYPEOFCREDITS: Musical Note 
CREDITSEARNED: 1 
INSTRUCTORNAME: Ray Bradbury 
INSTRUCTOREMAILADDRESS: [email protected] 
CLIENTNAME: Hogarth's Flying Circus 
COMMUNITYNAME: Captain's Club 
CERTIFICATESENTDATE: January, 06 2014 08:00:00 
DURATIONTYPE: 
DURATIONMINUTES: 
LOGIN: NavyB 

小提琴:https://dotnetfiddle.net/B7bMEe