0

I have a response coming from API call, I want to loop through the response to display it on UI.

IRestResponse response = client.Execute(request);
string responseContent = response.Content;
[
  {
    "details": {
      "ID": "STUD_111",
      "language": "en",
      "PersonalInfo": {
        "lastName": "Dias",
        "firstName": "Peter"
      }
    },
    "Score": {
      "attemptes": { "numOfAtmpt": 1 },
      "marks": {
        "total": 100,
        "min": 30,
        "achieved": "55"
      },
      "grade": {
        "card": [
          {
            "gradeObtained": "B",
            "passingGrade": {
              "Min": "C",
              "Avg": "B",
              "Max": "A"
            }
          }
        ]
      }
    }
  }
]

I am getting above response in responseContent variable. Need a way to display above information on UI.

2
  • 1
    Create a Model based on your response with -->(json2csharp.com) and then when you get a response from an API deserialize it "(var response = JsonConvert.DeserializeObject<List<Root>>(jsonString);" you should be able to loop through the response. Commented Sep 14, 2022 at 8:29
  • Hope this will help stackoverflow.com/questions/18490599/… Commented Sep 14, 2022 at 9:03

1 Answer 1

0

step1: Create a Model based on your response using this https://json2csharp.com/

  public class Root
  {
    public Details Details { get; set; }
    public Score Score { get; set; }
  }

  public class Details
  {
    public string ID { get; set; }
    public string Language { get; set; }
    public PersonalInfo PersonalInfo { get; set; }
  }

  public class Score
  {
    public Attemptes Attemptes { get; set; }
    public Marks Marks { get; set; }
    public Grade Grade { get; set; }
  }

 public class PersonalInfo
 {
   public string LastName { get; set; }
   public string FirstName { get; set; }
 }

 public class Attemptes
 {
   public int NumOfAtmpt { get; set; }
 }

 public class Marks
 {
   public int Total { get; set; }
   public int Min { get; set; }
   public string Achieved { get; set; }
 }

public class Grade
{
  public List<Card> Card { get; set; }
}

public class Card
{
  public string GradeObtained { get; set; }
  public PassingGrade PassingGrade { get; set; }
}

public class PassingGrade
{
  public string Min { get; set; }
  public string Avg { get; set; }
  public string Max { get; set; }
}

STEP 2:

var response = JsonConvert.DeserializeObject<List<Root>>(jsonString);

STEP 3:

  foreach (var item in response)
  {
      //do your magic here
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.