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", "[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"]
        ]
    }
}

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"]);
}
2
  • Are you sure you're using Json.Net? JavaScriptSerializer is a Microsoft-provided class (not part of Json.Net). Commented Nov 30, 2015 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. Commented Nov 30, 2015 at 16:10

4 Answers 4

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];
}
Sign up to request clarification or add additional context in comments.

1 Comment

"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.
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)
{
    //...
}

2 Comments

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

Comments

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

Output:

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

Fiddle: https://dotnetfiddle.net/B7bMEe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.