Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何从Arraylist命名JSON数组_Asp.net_Json_Asp.net Web Api - Fatal编程技术网

Asp.net 如何从Arraylist命名JSON数组

Asp.net 如何从Arraylist命名JSON数组,asp.net,json,asp.net-web-api,Asp.net,Json,Asp.net Web Api,我在分组JSON返回时遇到了一些问题 我需要给数组组一个名称 我在网上找到了很多信息,但总是让我困惑。 我在这方面没有太多经验,所以任何帮助都是有用的 谢谢你的意见 这是我的密码: public ArrayList Get() { ArrayList objs = new ArrayList(); try { FIREBIRD.ConnectionString = ConfigurationManager.

我在分组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"
}]
}

您将拥有一个对象,因此
Get
方法应该返回一个对象,而不是
ArrayList
。完成后,您需要

return new {
    sites = objs;
}
而不是

return objs;
编辑


}

嗨,Lajos Arpad,首先:谢谢你的回复。但我不明白。我应该改变物品的摆放方式还是?@Gregory不客气。你的代码几乎就是这样。只需更改即可返回objs;根据我的建议,并确保使用Get方法返回一个对象。您需要一个围绕objs的包装器。@Gregory请查看我编辑的答案。这是未经测试的,但完全表明了我的建议。@Gregory我很高兴问题已经解决了。
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();
    }
}