使用JSON.NET循环JSON结果

时间:2015-11-30 15:41:57

标签: c# asp.net json json.net

我有以下有效的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"]);
}

4 个答案:

答案 0 :(得分:1)

这样的事情应该有用,对不起它没有经过测试。

float **b = new float*[n]; //create local matrix
b[0] = new float[n*nCols] {0.0f}; // this line changed
for (int j = 1; j < n; j++) b[j] = b[j-1] + n;

答案 1 :(得分:0)

  1. Cenerate class
  2. 反序列化为此类的实例
  3. SELECT * FROM A 
        WHERE A.B_ID IN 
            (SELECT B.ID FROM B 
                WHERE ROWNUM < 2 ORDER BY ASC B.ID)
    

答案 2 :(得分: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);

答案 3 :(得分: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"", ""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

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