How to loop through JSON data?

paulson

New member
Joined
Jan 25, 2023
Messages
3
Programming Experience
5-10
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 -
JavaScript:
{
    "Outputdata": {
        "@xmlns": "[URL]http://xmlns.oracle.com/apps/pa/[/URL].......",
        "@xmlns:xsi": "[URL]http://www.w3.org/2001/XMLSchema-instance[/URL]",
        "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 -
C#:
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 -
C#:
            var responseBody = streamReader.ReadToEnd();
            var result = JsonConvert.DeserializeObject<OutputParameters>(responseBody);
Thanks,
 
Last edited by a moderator:
So what problem are you encountering? Wouldn't it just be a matter of looping through the TASK_O property of the result.Outputdata.TASK_O?

(It might help if you named your types and members actually correspond to your JSON so that is no confusion.)
 
My eyes..

So, you can use a JsonProperty attribute to specify the name in the json, which means you don't have to take a dump on c# naming conventions. If you paste your json into QuickType.io you'll get a nicely named set of classes and comments to tell you how to utilise the generated classes
 
I receive an error on

foreach (var item in result?.Outputdata?.TASK_0?.TASK_O_ITEM)

{

Object reference not set to an instance of an object.
 
So what problem are you encountering? Wouldn't it just be a matter of looping through the TASK_O property of the result.Outputdata.TASK_O?

(It might help if you named your types and members actually correspond to your JSON so that is no confusion.)
I am getting an error on
foreach (var item in result?.Outputdata?.TASK_0?.TASK_O_ITEM)
{

Object reference not set to an instance of an object.
 
With the debugger, did you check which of those properties was null? That would then tell you which part failed to deserialize correctly. JSON.NET will match JSON field name to C# property name.

As I said, your class names and properties are really hard to read. To make matters worst, I think some of those O's should be O's while others should be 0's (capital letter O vs number zero).
 
By the way, your code wasn't working because you cannot foreach a null, and some?.Conditional?.Access?.Route?.Like?.This can easily resolve to null. To prevent such being null you need to coalesce it with a non null of whatever the expression resolves to. For example if "This" is an int array you can:

C#:
 some?.Conditional?.Access?.Route?.Like?.This ?? Array.Emoty<int>()

So the foreach would enumerate 0 times over an empty int array if the conditional access expression resolves to null

This prevents the exception but doesn't correct the logic error that is causing the null in the first place; it's an example "defensive coding" - an if before the foreach, to make sure the expression isn't null would also work
 

Latest posts

Back
Top Bottom