locked
loop through json object and group to stringbuilder c# RRS feed

  • Question

  • JSON

    {"YesNotCheckedResponses": [
            {
              "QuestionID": "1",
              "Question": "Does your organisation have a health and safety policy?",
              "AnswerNotChkd": "It details who has day-to-day responsibility for implementing the policy."
            },
            {
              "QuestionID": "1",
              "Question": "Does your organisation have a health and safety policy?",
              "AnswerNotChkd": "It outlines how you propose to put in place your health and safety arrangements and how they are brought to the attention of staff."
            },
            {
              "QuestionID": "1",
              "Question": "Does your organisation have a health and safety policy?",
              "AnswerNotChkd": "It is signed and dated by the director and clearly displays a review date."
            },
            {
              "QuestionID": "2",
              "Question": "Does your organisation display a health and safety poster or have a means to provide basic information to staff?",
              "AnswerNotChkd": "We are confident that all staff are aware of the primary duties of employers and employees set out in these documents."
            }
          ]}

    How would I filter through this and output to my stringbuilder(c#), grouping by QuestionID and Question, So output to stringbuilder would be something like;

    var myResult = new stringbuilder();

    myResult.Append("(");

    myResult.Append("
    QuestionID: 1, Question: Does your organisation have a health and safety policy?, AnswerNotChkd: It details who has day-to-day responsibility for implementing the policy.\n It outlines how you propose to put in place your health and safety arrangements and how they are brought to the attention of staff.\n It is signed and dated by the director and clearly displays a review date.\n QuestionID: 2, Question: Does your organisation display a health and safety poster or have a means to provide basic information to staff? AnswerNotChkd: We are confident that all staff are aware of the primary duties of employers and employees set out in these documents.\n");

    myResult.Append(")");

    Tuesday, November 13, 2018 2:59 PM

Answers

  • It depends upon what JSON library you're using. .NET doesn't really have its own (ignoring the DataContractSerializer). Most people use JSON.NET right now. That library allows you to deserialize to strong or anonymous types.

    Personally I would use a strong type to deserialize to but the JSON isn't exactly well laid out and it seems like this is just a subset of the actual JSON so defining a strongly typed structure may be difficult.

    class Questions
    {
        public IEnumerable<ResponseQuestion> YesNotCheckedResponses { get; set; }
    }
    
    class ResponseQuestion
    {
        public int QuestionID { get; set; }
    
        public string Question { get; set; }
    
        public string AnswerNotChkd { get; set; }
    }
    
    //Usage
    var json = File.ReadAllText("test.json");
    var data = JsonConvert.DeserializeObject<Questions>(json);
    
    var questions = from d in data.YesNotCheckedResponses
                    group d by new { d.QuestionID, d.Question } into g
                    select new { Id = g.Key.QuestionID, Question = g.Key.Question, Answers = g.Select(x => x.AnswerNotChkd).ToArray() };
    
    var builder = new StringBuilder();
    foreach (var question in questions)
    {
        builder.Append($"QuestionID: {question.Id}, Question: {question.Question}, AnswerNotChkd: {String.Join("\n", question.Answers)}");
    };


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 13, 2018 6:35 PM

All replies

  • It depends upon what JSON library you're using. .NET doesn't really have its own (ignoring the DataContractSerializer). Most people use JSON.NET right now. That library allows you to deserialize to strong or anonymous types.

    Personally I would use a strong type to deserialize to but the JSON isn't exactly well laid out and it seems like this is just a subset of the actual JSON so defining a strongly typed structure may be difficult.

    class Questions
    {
        public IEnumerable<ResponseQuestion> YesNotCheckedResponses { get; set; }
    }
    
    class ResponseQuestion
    {
        public int QuestionID { get; set; }
    
        public string Question { get; set; }
    
        public string AnswerNotChkd { get; set; }
    }
    
    //Usage
    var json = File.ReadAllText("test.json");
    var data = JsonConvert.DeserializeObject<Questions>(json);
    
    var questions = from d in data.YesNotCheckedResponses
                    group d by new { d.QuestionID, d.Question } into g
                    select new { Id = g.Key.QuestionID, Question = g.Key.Question, Answers = g.Select(x => x.AnswerNotChkd).ToArray() };
    
    var builder = new StringBuilder();
    foreach (var question in questions)
    {
        builder.Append($"QuestionID: {question.Id}, Question: {question.Question}, AnswerNotChkd: {String.Join("\n", question.Answers)}");
    };


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 13, 2018 6:35 PM
  • Yes you are right, it's not a complete JSON formed object. It was only a snippet of what I was trying to demonstrate.

    This works ideally to my needs, you have saved my another headache, so thanks!



    • Edited by big2dg Wednesday, November 14, 2018 2:09 PM
    Wednesday, November 14, 2018 8:20 AM