How to loop through JSON data?

Paul 0 Reputation points
2023-01-25T17:43:30.51+00:00

Hi,
I am calling an api and this is the return data.
Could you please help me to loop through the JSON data?

The JSON string -

{
    "Outputdata": {
        "@xmlns": "http://xmlns.oracle.com/apps/pa/.......",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "TASK_0": {
            "TASK_O_ITEM": [
                {
                    "CODE": "123",
                    "TASK_NAME": "abc"
                },
                {
                    "CODE": "456",
                    "TASK_NAME": "def"
                },                {
                    "CODE": "789",
                    "TASK_NAME": "ghi"
                }
            ]
        },
        "ERROR_O": null
    }
}

My Classes -

public class Outputdata
{
    public TASK_O TASK_O{ get; set; }
    public string ERROR_O { get; set; }
}

public class TASK_O
{
    public List<TASK_O_ITEM> TASK_O{ get; set; }
}

public class TASK_O_ITEM
{
   public string CODE { get; set; }
    public string NAME { get; set; }
}

Code - 

            var responseBody = streamReader.ReadToEnd();
            var result = JsonConvert.DeserializeObject<OutputParameters>(responseBody);

Thanks,
Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-01-25T18:04:35.68+00:00

    The names are a little confusing because of O and 0 ! But you are almost there, here is the fixed code which at least parses your sample json data correctly:

    public class MyModel
    {
        public Outputdata Outputdata { get; set; }
    }
    
    public class Outputdata
    {
        public TASK_0 TASK_0 { get; set; }
        public string ERROR_O { get; set; }
    }
    
    public class TASK_0
    {
        public List<TASK_O_ITEM> TASK_O_ITEM { get; set; }
    }
    
    public class TASK_O_ITEM
    {
        public string CODE { get; set; }
        public string TASK_NAME { get; set; }
    }
    

    And the usage:

    var responseBody = "the body from wherever you read";
    var result = JsonConvert.DeserializeObject<MyModel>(responseBody);
    foreach (var item in result?.Outputdata?.TASK_0?.TASK_O_ITEM)
        Console.WriteLine($"{item.CODE} - {item.TASK_NAME}");
    
    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-01-25T18:56:07.8066667+00:00

    most likely the json is really mapped objects, so you will see (or some variant:

    {
        "Outputdata": {
            "TASK_0": { 
               "TASK_0_ITEM": { ...},
               "TASK_1_ITEM": { ...}
            },
            "ERROR_0": null,
            "TASK_1": {
               "TASK_0_ITEM": { ...},
               "TASK_1_ITEM": { ...}
            },
            "ERROR_1": null
         }
    }
    

    so in c# you would use dictionaries rather than objects

    
    public class Root
    {
        public Dictionary<string,object> Outputdata { get; set; }
    }
    
    public class TASK_LIST : Dictionary<string, List<TASK_ITEM>> 
    {
    }
    
    
    public class ERROR 
    {
    }
    
    public class TASK_ITEM
    {
       public string CODE { get; set; }
       public string NAME { get; set; }
    }
    
    

    where ObjectData will a dictionary of ERROR, string and TASK_LIST objects

    you will need a custom deserialize


  3. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-01-26T06:41:12.96+00:00

    Hi @Paul

    My thoughts are:

    1. Declare classes and lists.
    2. Assign a value to the data.
    3. Generate JSON variables.
    4. Print it out

    For demonstration purposes, I printed the JSON data as string data.

    In order to keep the same format as JSON, I set an option.

    var options = new JsonSerializerOptions { WriteIndented = true };

    string jsonString = System.Text.Json.JsonSerializer.Serialize(outputdata, options);

    Of course, you can also print it directly.

    You can refer to the following code:

    public class TASK_O
    {
        public List<TASK_O_ITEM> TASK_O_ITEM { get; set; }
    }
    public class Outputdata
    {
        public TASK_O TASK_O { get; set; }
        public string ERROR_O { get; set; }
    }
    public class Example
    {
        public Outputdata Outputdata { get; set; }
    }
    public class TASK_O_ITEM
    {
        public string CODE { get; set; }
        public string NAME { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            var task_o_item = new List<TASK_O_ITEM> {
                new TASK_O_ITEM
                {
                    CODE = "123",
                    NAME = "ABC"
                },
                new TASK_O_ITEM
                {
                    CODE = "456",
                    NAME = "DEF"
                },
                new TASK_O_ITEM
                {
                    CODE = "789",
                    NAME = "GHI"
                },
            };
            var task_O = new TASK_O
            {
                TASK_O_ITEM = task_o_item
            };
            var outputdata = new Outputdata
            {
                TASK_O = task_O,
                ERROR_O = null
            };
            var example = new Example
            {
               Outputdata=outputdata
            };
            var options = new JsonSerializerOptions { WriteIndented = true };
            string jsonString = JsonSerializer.Serialize(example, options);
            Console.WriteLine(jsonString);
            Console.Read();
        }
    }
    

    Result:

    Test5

    Best Regards

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.