Как назвать JSON Array от Arraylist - PullRequest
1 голос
/ 02 ноября 2019

У меня возникли проблемы с группировкой возврата JSON.

Мне нужно дать группе массивов имя.

Я нашел много информации в Интернете, но это меня смущает. У меня нет большого опыта в этом, поэтому любая помощь полезна.

Спасибо за ввод!

Вот мой код:

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

Текущий доход:

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

Что должно возвращаться:

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

1 Ответ

2 голосов
/ 02 ноября 2019

У вас будет объект, поэтому метод Get должен возвращать объект вместо ArrayList. Когда это будет сделано, вам нужно будет

return new {
    sites = objs;
}

вместо

return objs;

РЕДАКТИРОВАТЬ

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

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...