【问题标题】:How to retrieve json data using jQuery and Json.NET?如何使用 jQuery 和 Json.NET 检索 json 数据?
【发布时间】:2013-06-18 21:45:23
【问题描述】:

我在http://james.newtonking.com/projects/json/help/ 的“序列化和反序列化 Json | 序列化集合”中看到了这个代码示例:

Product p1 = new Product
{
  Name = "Product 1",
  Price = 99.95m,
  ExpiryDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
};
Product p2 = new Product
{
  Name = "Product 2",
  Price = 12.50m,
  ExpiryDate = new DateTime(2009, 7, 31, 0, 0, 0, DateTimeKind.Utc),
};

List<Product> products = new List<Product>();
products.Add(p1);
products.Add(p2);

string json = JsonConvert.SerializeObject(products, Formatting.Indented);
//[
//  {
//    "Name": "Product 1",
//    "ExpiryDate": "2000-12-29T00:00Z",
//    "Price": 99.95,
//    "Sizes": null
//  },
//  {
//    "Name": "Product 2",
//    "ExpiryDate": "2009-07-31T00:00Z",
//    "Price": 12.50,
//    "Sizes": null
//  }
//]

到目前为止一切顺利;但是如何从 jQuery 中检索这个输出呢?是否可以有一个名为 JsonizedProducts.cshtml 的文件:

@{
    return GetProductsAsJson(); // returns the string named "json" above
}

...并使用jQuery如(伪代码):

$.getJson('\JsonizedProducts') {
    // loop through the json, building html with injected product values
}

?还是……?

更新

我找到了很多执行此操作所需的客户端 jQuery 代码示例;这是我缺少的服务器端方面。如果我用 .getJSON("\Bla\Blee.js", someVal, someFunc) 之类的方法调用 .getJson(path, data, callback),Blee.js 将如何传回 JSON 数据?被称为 diesbzg 的文件的职责是什么?

更新 2

这是我能做到的/一种方式吗(getPlatypus() 和 getWombat() 是其他类中返回 List 的方法):

// This code is in the file that .getJson() calls; the data (args) .getJson calls has "mammalRequested" Using the Web.Helpers Json helper
string jsonResult;
if (mammalRequested == "Platypus") {
    jsonResult = Json.Encode(getPlatypus); 
}
else if (mammalRequested == "Wombat") {
    jsonResult = Json.Encode(getWombat); 
}
Response.Write(jsonResult);

?

更新 3

这不是我的问题的答案,因为我提到了 Json.NET,但这很可能是最好的解决方案:我知道有一种方法可以从 C# 传回 jsonized 数据(Brind 的书在第 168-170 页显示了如何),但可能最明智的(最简单的)是简单地调用一个包含 json 数据的 .js 文件,如下所示:

$.getJson('someFile.js', 'someArg', someCallbackFunction);

创建 Json 文件的最简单方法(对我来说)是什么?首先,创建一个csv文件,而不是使用CSV2Json转换器,例如来自http://www.convertcsv.com/csv-to-json.htm

然后,当然,使用 .each(data) 循环遍历 json 记录并“获取它们”。

【问题讨论】:

    标签: jquery asp.net razor json.net


    【解决方案1】:

    我猜你正在使用 MVC 框架,因为你在谈论 .cshtml 文件。如果是这样,请在您的控制器中创建一个方法,如下所示:

    [HttpGet]
    public JsonResult GetProductsAsJson()
    {
        List<Product> products = repository.GetProducts(); //or whatever code generates the list
        return Json(products, JsonRequestBehaviour.AllowGet);
    }
    

    然后在您的 javascript 中,执行您在上面所做的操作! url 将像往常一样是方法名称和控制器名称。如果您将 javascript 放在 .chstml 页面中,您可以使用 Url.Action("GetProductsAsJson", "MyController') 来呈现方法的 url。

    编辑:

    好的,基于cmets,试试这个:

    在项目的 .cs 文件中创建命名空间和类:

    namespace MyThings
    {
        public class DbRepository
        { 
             public static string GetProductsJSON()
             {
                   //code to create json string
                   return json;
             }
        }
    }
    

    然后在您的 .cshtml 文件中在页面顶部添加 using 指令:

    @using MyThings
    

    并调用razor块中的方法设置变量,然后使用razor循环遍历:

    @{ 
        List<Products> prods = DbRepository.GetProductsJSON();
     }
    
     <ul>
         @foreach (var prod in prods)
          {
               <li>@prod.Name</li>
          }
     </ul>
    

    不是网页专家,但我认为这会奏效。

    祝你好运!

    【讨论】:

    • 可能使用 MVC,但至少到目前为止,这是一个简单的 Web Pages 站点。不错的答案,不过!我可能会使用它。
    • 在这种情况下,您不能在页面顶部使用 @{} 块来创建产品列表,然后使用标准 razor 语法循环它们并根据需要呈现 HTML 吗?还是您绑定到特定事件?
    • 我希望做这样的事情,是的;但是,我不知道我需要在那里放什么。我原来的帖子中有一个想法。根据你刚刚写的,我想知道是否有可能:@{ public string GetProductsAsJson() { bla... } }
    猜你喜欢
    • 2016-03-07
    • 2016-03-14
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多