c# – Json没有以所需的模式返回正确的值

我需要这个作为我的json回归.这些数据是从数据库中提取的,但目前我正在使用静态数据.

 {
        "data": [
            {
                "id": 1,
                "latitude":17.3700,
                "longitude": 78.4800,
                "gallery":
                    [
                        "assets/img/items/1.jpg"
                    ]
            }
        ]
    }

我已经在我的代码中尝试了这个但是我没有得到理想的结果.

[WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public List<> getData()
    {
        Account account = new Account
        {
            id = 1,
            latitude = "17.3700",
            longitude ="78.4800",
            gallery = new List<string>
                  {
                    "assets/img/items/1.jpg",
                     "assets/img/items/2.jpg",
                  }
        };
        return new JavaScriptSerializer().Serialize(account);
    }

    public class Account
    {
        public int id { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public IList<string> gallery  { get; set; }
    }

结果:

{
 "id":2,
 "latitude":"17.3700",
 "longitude":"78.4800",
 "gallery":["assets/img/items/1.jpg","assets/img/items/2.jpg"]
}

最佳答案 您需要使用data属性创建一个新类:

public class Result { public object[] Data { get; set; } }

并返回:

public string getData()
{
    Result result = new Result
    {
        Data = new [] { new Account { id = 1, ... } }
    };

    return new JavaScriptSerializer().Serialize(result);
}
点赞