AnsweredAssumed Answered

Parsing a JSON object using JSON.net to save data points from Section API?

Question asked by Parminder Jhaj on Sep 21, 2016
Latest reply on Jan 31, 2017 by rogrant@nmsu.edu

// the code above this line works properly and is calling a web client to set this up with appropriate header lines

string strResult = wc.DownloadString(URI);

JObject data = JObject.Parse(strResult);

 

var courseInfo = from x in data[""][0] // have tried either [""] or [0] or [""][0]

                                                     select new

                                                     {

                                                         id = (string)x["id"] ?? "",

                                                         course_id = (string)x["course_id"] ?? "",

                                                         name = (string)x["name"] ?? "",

                                                         start_at = (string)x["start_at"] ?? "",

                                                         end_at = (string)x["end_at"] ?? "",

                                                         nonxlist_course_id = (string)x["nonxlist_course_id"] ?? "",

                                                         sis_section_id = (string)x["sis_section_id"] ?? "",

                                                         sis_course_id = (string)x["sis_course_id"] ?? "",

                                                         integration_id = (string)x["integration_id"] ?? "",

                                                         sis_import_id = (string)x["sis_import_id"] ?? ""

                                                     };

// when debugging the query causes an exception and says An unhandled exception of type 'System.NullReferenceException'. Additional information: Object reference not set to an instance of an object.

I have never used JSON.net before and never worked with JSON objects so I am not sure on how to fix this so that my foreach loop later can save the data points.

 

// another thing I noticed is when the JSON object is parsed it creates two sets of curly braces

     EX) {{ // json information }}

Would this be a reason as to why the query fails? Or does it not necessarily matter?

Outcomes