Gregory
Gregory

Reputation: 159

How to name JSON Array from Arraylist

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"
}]
}

Upvotes: 1

Views: 125

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76551

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();
    }
}

}

Upvotes: 2

Related Questions