[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Generate Array from JSON to insert records into data base


I'm trying to generate an array of values that I can insert into my database from a JSON string that I receive.

So far:  I'm able to generate a dictionary object with the code

Dictionary<string, object> dict = Utilities.Dictionary_Get_From_JSON(ref data_value);

The pairs in the dictionary look something like this:




It's at this point that I start hitting the wall.

I am trying to generate an array or table that looks like this

ID             Type          student_response  result  
Question9_1    true-false    t                 correct
Question9_1    true-false    f                 correct
Question9_1    true-false    t                 wrong

and then insert the data from that array into my database:

foreach row in table/array
   InsertRecord(id, type, student_response, result)

Any suggestions on how to do this would be greatly appreciated.

  • 3
1 Solution
RBSAuthor Commented:
whoops, my array should have looked like...

ID             Type          student_response  result  
Question9_1    true-false    t                 correct
Question10_1    true-false    f                 correct
Question11_1    true-false    t                 wrong

Although I appreciate what you've posted so far it would be easier to help if I could see:

1. The original JSON serialized string (maybe thats data_value)
2. This function Utilities.Dictionary_Get_From_JSON(ref data_value)
3. A simple version of this class cmi_interactions if you have it.

The more clear I can see your Dictionary the better.

If you provide that then I'd ask you the question (for going forward)... Are you sure using/casting to Dictionary<string, object> is the way you want to go? Is there flex on that or not?
RBSAuthor Commented:
Thanks ddayx10 - I've solved the problem on my own - the dictionary seemed to work fine.  Appreciate your response, though

RBSAuthor Commented:
Here's the solution I came up with:

private static bool UpdateAnswers(int RecordId, string data_value)

        Record record = DataAccess.Records_Get(RecordId);

        //Get dictionary of all terms imported from JSON string data_value
        JavaScriptSerializer ser = new JavaScriptSerializer();
        SortedDictionary<string, object> dict = ser.Deserialize<SortedDictionary<string, object>>(data_value);

        int length = dict.Count;
        string cmi;
        string aNumber;

        //Create a Sub Dictionary containing only the terms that have cmi.interactions.N in them
        SortedDictionary<string, string> studentAnswer = new SortedDictionary<string, string>();

        //From the studentAnswer Dictionary, we will generate an array that will contain the data to be entered in the db
        //Create the array that will store the values of the student interactions 
        string[,] scorray = new string[50, 5];
            foreach (KeyValuePair<string, object> pair in dict)
                //Create Dictionary studentAnswer with only the com.interactions   
                //We will use this as a basis to add the student answers to the database
                    cmi = pair.Key.Substring(0, 8);
                    aNumber = pair.Key.Substring(17, 1);
                    if (cmi == "cmi.inte" & pair.Key.Substring(17, 1) != "_")
                        studentAnswer.Add(pair.Key.ToString(), pair.Value.ToString());
                catch { }

            foreach (KeyValuePair<string, string> pair in studentAnswer)
                //word[0] = cmi
                //word[1] = interactions
                //word[2] = interaction Number (N)
                //word[3] = key - e.g.  id, type, student_response, result

                string chars = @".";
                string[] words = pair.Key.ToString().Split(chars.ToCharArray());

                switch (words[3].ToString())
                    case "id":
                        scorray[Convert.ToInt32(words[2]), (int)SCORMField.id] = pair.Value;

                    case "type":  //“true-false”, “choice”, “fill-in”, “long-fill-in”, “matching”, “performance”, “sequencing”, “likert”, “numeric” or “other”
                        scorray[Convert.ToInt32(words[2]), (int)SCORMField.type] = pair.Value;

                    case "student_response":   //string 
                        scorray[Convert.ToInt32(words[2]), (int)SCORMField.student_response] = pair.Value;

                    case "result":  //correct, wrong “incorrect”, “unanticipated”, “neutral” or real number accurate to 7 significant digits
                        scorray[Convert.ToInt32(words[2]), (int)SCORMField.result] = pair.Value;



Open in new window

Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now