1

I am having some trouble in grouping a JSON return.

I need to give the array group a name.

I've found a lot of information online but it keeps confusing me. I haven't got a lot of experience in this so any help is useful.

Thanks for the input!

Here's my code:

    public ArrayList Get()
    {
        ArrayList objs = new ArrayList();

        try
        {
            FIREBIRD.ConnectionString = ConfigurationManager.ConnectionStrings["Firebird"].ConnectionString;
            FIREBIRD.Open();

            FbDataReader reader = null;
            FbCommand command = new FbCommand("SOME QUERY", FIREBIRD);
            reader = command.ExecuteReader();

            if (reader.HasRows == true)
            {
                while (reader.Read())
                {
                    objs.Add(new
                    {
                        id = reader["CODE"],
                        name = reader["TITE"],
                        address = reader["ADRES"],
                        postal_code = reader["POSTAL"],
                        city = reader["CITY"]
                    });
                }
                return objs;
            }
            else
            {
                objs.Add(new { ERROR = "NO DATA AVAILABLE" });
                return objs;
            }
        }

        catch (Exception)
        {
            throw;
        }

        finally
        {
            FIREBIRD.Close();
        }
    }
}

The current return:

[{
    "id": "code",
    "name": "name",
    "address": "adres",
    "postal_code": "1234",
    "city": "city"
}]

What it should return:

{"sites":
[{
    "id": "code",
    "name": "name",
    "address": "adres",
    "postal_code": "1234",
    "city": "city"
}]
}

1 Answer 1

2

You will have an object, so the Get method should return an object instead of ArrayList. When that's done, you will need to

return new {
    sites = objs;
}

instead of

return objs;

EDIT

public Object Get()
{
    ArrayList objs = new ArrayList();

    try
    {
        FIREBIRD.ConnectionString = ConfigurationManager.ConnectionStrings["Firebird"].ConnectionString;
        FIREBIRD.Open();

        FbDataReader reader = null;
        FbCommand command = new FbCommand("SOME QUERY", FIREBIRD);
        reader = command.ExecuteReader();

        if (reader.HasRows == true)
        {
            while (reader.Read())
            {
                objs.Add(new
                {
                    id = reader["CODE"],
                    name = reader["TITE"],
                    address = reader["ADRES"],
                    postal_code = reader["POSTAL"],
                    city = reader["CITY"]
                });
            }
        }
        else
        {
            objs.Add(new { ERROR = "NO DATA AVAILABLE" });
        }
        return new {
            sites = objs;
        }
    }

    catch (Exception)
    {
        throw;
    }

    finally
    {
        FIREBIRD.Close();
    }
}

}

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Lajos Arpad, first of all: thank you for your reply. But i'm not following here. Should I change the way I nest the items or?
@Gregory you are welcome. Your code is almost good as it is. Just change returns objs; to my recommendation and make sure that you return an object with your Get method. You need a wrapper around objs.
@Gregory please take a look at my edited answer. It is untested, but quite shows what I am suggesting.
@Gregory I'm glad that the problem has been solved.

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.