skip to Main Content

I have the following JSON in C# :

 string extractedText = "   [
      {
        "mainQuestion": "Question 5: Attempt Any 2 [5 x 2 = 10]",
        "question": "Write a C program to define a structure called student that contains the following members: name, age, and marks. Then, create an array of students and print the details of all students.",
        "marks": "5",
        "correctAnswer": "To solve this problem, we can define a structure called `student` that contains three members: `name`, `age`, and `marks`. Then, we can create an array of students and use a loop to print the details of each student. Here is an example program that demonstrates how this can be done:",
        "example": `
    #include <stdio.h>
    
    // Define the student structure
    struct student {
        char name[50];
        int age;
        float marks;
    };
    
    // Create an array of students
    struct student students[3];
    
    // Get the details of each student from the user
    for (int i = 0; i < 3; i++) {
        printf("Enter the name of student %d: ", i + 1);
        scanf("%s", students[i].name);
    
        printf("Enter the age of student %d: ", i + 1);
        scanf("%d", &students[i].age);
    
        printf("Enter the marks of student %d: ", i + 1);
        scanf("%f", &students[i].marks);
    }
    
    // Print the details of all students
    printf("nDetails of all students:n");
    for (int i = 0; i < 3; i++) {
        printf("nStudent %d:n", i + 1);
        printf("Name: %sn", students[i].name);
        printf("Age: %dn", students[i].age);
        printf("Marks: %.2fn", students[i].marks);
    }
    `
      }
    ]"

I have created a class as follows :

public class QuestionAnswer
{
    public string mainQuestion { get; set; }
    public string question { get; set; }
    public string marks { get; set; }
    public string correctAnswer { get; set; }
    public string example { get; set; }
}
var questionsandAnswers = JsonConvert.DeserializeObject<QuestionAnswer>(extractedText);

As you can see, I am trying to Deserialize.I have gone through this and this but in my case its not working because as you can see there are lot of special characters in the example property which are causing me a lot of trouble to Deserialize. I have tried to replace the backtick(just after the example property starts and also at the end) with " and "" but nothing seems to work. I keep getting errors.

What else should I do to Deserialize the above string ?
Thanks a lot.

2

Answers


  1. Chosen as BEST ANSWER

    I have found a dirty solution at the moment. This is how it looks :

     string strMark = ""; string line = "";
     bool isExmapleFound = false;
     using (StringReader reader = new StringReader(responceText))
     {
         while ((strMark = reader.ReadLine()) != null)
         {
             if(isExmapleFound)
             {
                 string[] lineStrings = strMark.Split(" ");
                 for (int i = 0; i < lineStrings.Length; i++)
                 {
                     line += lineStrings[i] + " ";
                 }
                 continue;
             }
    
             if(strMark.Contains(""example":"))
             {
                 isExmapleFound = true;
                 continue;   
             }
         }
     }
    

    The above does extract the "example" property but since its a code, I have to write a separate code to make it look like a code(I mean I have to format it). Also, I have to write additional code to extract the remaining properties as well using the same style. I am still figuring out if i can find better solution. If anybody has, please let me know. Thanks.


  2. Use a string literal to define strings with many special characters directly in your code. You can do this by enclosing your string in triple quotes (or more, if needed).

    Example:

    string someText = """This is a text
    with line breaks and some special characters
     / @ ' "
    """
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search