Newtonsoft JSON - create JArray in JArray

13,992

You can achieve the same result (JArray in JArray) using regular C# classes and at the end serialize to JSon.

I posted a sample in Github; here a fragment of the code that produces your expected output:

        var Surveys = new List<SurveytrackD>();

        Surveys.Add( new SurveytrackD { id = "26", appsurvey = "1", fk_curriculumid = "70", status = "Completed", learnerid = "240"  } );
        Surveys.Add( new SurveytrackD { id = "27", appsurvey = "1", fk_curriculumid = "71", status = "Completed", learnerid = "241" });


        foreach (var survey in Surveys)
        {
            survey.questions = new List<Question>();

            survey.questions.Add(new Question { questionid = "1", feedback = "0" });
            survey.questions.Add(new Question { questionid = "2", feedback = "1" });
        }

        var json = JsonConvert.SerializeObject(Surveys, Formatting.Indented);

        Console.WriteLine(json);

The output is:

[
  {
    "fk_clientid": null,
    "learnerid": "240",
    "appsurvey": "1",
    "id": "26",
    "fk_curriculumid": "70",
    "status": "Completed",
    "lastaccessedon": null,
    "questions": [
      {
        "feedback": "0",
        "questionid": "1"
      },
      {
        "feedback": "1",
        "questionid": "2"
      }
    ]
  },
  {
    "fk_clientid": null,
    "learnerid": "241",
    "appsurvey": "1",
    "id": "27",
    "fk_curriculumid": "71",
    "status": "Completed",
    "lastaccessedon": null,
    "questions": [
      {
        "feedback": "0",
        "questionid": "1"
      },
      {
        "feedback": "1",
        "questionid": "2"
      }
    ]
  }
]
Share:
13,992
gofor.net
Author by

gofor.net

Updated on June 04, 2022

Comments

  • gofor.net
    gofor.net almost 2 years

    I am trying to create JSON array using Newtonsoft JSON API but its giving me error. I want to achieve structure like

    [
    
        {
            "id":"26",
            "appsurvey":"1",
            "fk_curriculumid":"70",
            "status":"Completed",
            "lastaccessedon":"2014-06-20 09:18:54",
            "questions":[
                {
                    "feedback":"6",
                    "questionid":"1"
                },
                {
                    "feedback":"8",
                    "questionid":"2"
                },
                {
                    "feedback":"1",
                    "questionid":"3"
                }
            ],
            "fk_clientid":"24",
            "learnerid":"260"
        }
    
    ]
    

    I want ot add questions array for multiple time but it is giving me error Can not add property questions to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.

    Here is my code:

    JArray surveytrackingA = new JArray();
    /*code to add
    [
    
        {"id":"26",
            "appsurvey":"1",
            "fk_curriculumid":"70",
            "status":"Completed",
            "lastaccessedon":"2014-06-20 09:18:54"}]
    */
    for (int i = 0; i < surveytrackingA.Count; i++)
    {
    
        JObject surveytrackD = (JObject)surveytrackingA[i];
        string queryOne = "select * from table101 where fk_curriculumid='"
                + surveytrackD["fk_curriculumid"].ToString()
                + "' and fk_surveyid='"
                + surveytrackD["appsurvey"].ToString() + "'";
    
        JArray questionsA = new JArray();
    
        using (var stmt = await App.localDB.PrepareStatementAsync(queryOne))
        {
            while (await stmt.StepAsync())
            {
                JObject questionD = new JObject();
                questionD.Add("questionid", stmt.GetTextAt(5));
                questionD.Add("feedback", stmt.GetTextAt(6));
                questionsA.Add(questionD);
            }
        }                    
        surveytrackD.Add("questions", questionsA); /*error occurred here when second question array is getting inserted in surveyTrackD*/
        surveytrackingA.Add(surveytrackD);
    }
    

    Can anyone please correct me. Thanks in advance.

    Update: surveytrackD have the json data,

    {
      "fk_clientid": "24",
      "learnerid": "260",
      "appsurvey": "1",
      "id": "26",
      "fk_curriculumid": "70",
      "status": "completed",
      "lastaccessedon": "2014-06-20 09:18:54"
    }