Generate Array from JSON to insert records into data base

Posted on 2011-10-27
Last Modified: 2012-05-12

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.

Question by:RBS

    Author Comment

    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

    LVL 20

    Expert Comment

    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?

    Author Comment

    Thanks ddayx10 - I've solved the problem on my own - the dictionary seemed to work fine.  Appreciate your response, though


    Accepted Solution

    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)] = 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

    LVL 19

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now