使用JSON.NET循环JSON结果



我有下面的有效JSON,我需要遍历结果。我使用的是JSON.NET和c#。我可以获得SUCCESS的值,但我不知道如何访问任何指导都会有帮助。

{
    "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", "lynn@domain.com", "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", "lynn@domain.com", 
             "Hogarth's Flying Circus", "Captain's Club", null, null, null, 
             "lynn@domain.com"],
            ["Beane", "Navy", "lynn@domain.com", "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", "lynn@domain.com", 
             "Hogarth's Flying Circus", "Captain's Club", "January, 06 2014 08:00:00", 
             null, null, "NavyB"]
        ]
    }
}

我可以通过使用这个代码块获得SUCCESS值

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"]);
}

这样的东西应该可以工作,很抱歉没有经过测试。

JArray data_list = (JArray)jsonObj["COLUMNSANDDATA"]["DATA"];
foreach (JObject data in data_list) {
    string col_0 = (string)data[0];
}
  1. Cenate类
  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)
{
    //...
}

我强烈建议您使用自动生成的类,以便最轻松地导航和使用这些类。

看看这篇文章如何从JSON对象字符串自动生成C#类文件,它将帮助您完成自动生成类形式JSON和xml。顺便说一句,它将来会对你有所帮助。

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

您可以循环使用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"", ""lynn@domain.com"", ""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"", ""lynn@domain.com"", ""Hogarth's Flying Circus"", ""Captain's Club"", null, null, null, ""lynn@domain.com""],
            [""Beane"", ""Navy"", ""lynn@domain.com"", ""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"", ""lynn@domain.com"", ""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: lynn@domain.com
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: lynn@domain.com
CLIENTNAME: Hogarth's Flying Circus
COMMUNITYNAME: Captain's Club
CERTIFICATESENTDATE:
DURATIONTYPE:
DURATIONMINUTES:
LOGIN: lynn@domain.com
LASTNAME: Beane
FIRSTNAME: Navy
EMAILADDRESS: lynn@domain.com
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: lynn@domain.com
CLIENTNAME: Hogarth's Flying Circus
COMMUNITYNAME: Captain's Club
CERTIFICATESENTDATE: January, 06 2014 08:00:00
DURATIONTYPE:
DURATIONMINUTES:
LOGIN: NavyB

Fiddle:https://dotnetfiddle.net/B7bMEe

相关内容

  • 没有找到相关文章

最新更新