0

I have the below valid JSON and I need to loop through the results. I am using JSON.NET and c#. I am able to get the value for SUCCESS, but I do not know how to access Any guidance would be helpful.

{
    "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"]
        ]
    }
}

I am able to get the SUCCESS Value by using this block of code

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"]);
}
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
Chris Lombardi
  • 861
  • 2
  • 14
  • 31
  • Are you sure you're using [Json.Net](http://www.newtonsoft.com/json)? [`JavaScriptSerializer`](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx) is a Microsoft-provided class (not part of Json.Net). – Brian Rogers Nov 30 '15 at 16:06
  • Yes. I installed JSON.NET since a lot of the examples I have looked at use it. The project was originally created using just MS provided class libraries though. – Chris Lombardi Nov 30 '15 at 16:10

4 Answers4

1

Something like this should work, sorry it's not tested.

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

foreach (JObject data in data_list) {
    string col_0 = (string)data[0];
}
TommyGunn32
  • 924
  • 1
  • 8
  • 22
  • "Cannot apply indexing with [] to an expression of type 'object', I see what you are trying to do though and since I only need one or two values this is the route I am going to pursue. – Chris Lombardi Nov 30 '15 at 16:00
0
  1. Cenerate class
  2. Deserialize to instance of this class

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)
{
    //...
}
Backs
  • 24,430
  • 5
  • 58
  • 85
  • Is this really necessary if the user only wants, for example, a single property for each item? Imagine if the JSON was much larger... – Umair Nov 30 '15 at 15:54
  • @Umair it depends. I don't know all requirements. So, I show only one way, how it can be solved. Of couse, there are many cases where my answer is bad, but there are a lot where it's good. And here, I think, it's good enough – Backs Nov 30 '15 at 15:57
0

I strongly recommend you to use autogenerated classes for most comfortable navigating and using theese classes.

Look at this article How to auto-generate a C# class file from a JSON object string it will help you to get through auto generating classe form json and xml. Btw it will help you in future.

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

Community
  • 1
  • 1
Mitklantekutli
  • 410
  • 1
  • 3
  • 16
0

You can loop through the JSON and dump everything out like this:

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

Output:

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

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300