【问题标题】:How to persist this json in database?如何将此json保存在数据库中?
【发布时间】:2012-08-07 20:42:16
【问题描述】:

我的 Json 代码如下

[
    {"group":{"key":"Chinese","title":"Chinese","shortTitle":"Chinese","recipesCount":0,"description":"Chinese cuisine is any of several styles originating from regions of China, some of which have become increasingly popular in other parts of the world – from Asia to the Americas, Australia, Western Europe and Southern Africa. The history of Chinese cuisine stretches back for many centuries and produced both change from period to period and variety in what could be called traditional Chinese food, leading Chinese to pride themselves on eating a wide range of foods. Major traditions include Anhui, Cantonese, Fujian, Hunan, Jiangsu, Shandong, Szechuan, and Zhejiang cuisines. ","rank":"","backgroundImage":"images/Chinese/chinese_group_detail.png", "headerImage":"images/Chinese/chinese_group_header.png"},
    "key":1000,
    "title":"Abalone Egg Custard",
    "shortTitle" : "Abalone Egg Custard", 
    "serves":4,
    "perServing":"65kcal / 2.2g fat",
    "favorite":false,
    "rating": 3 , 
    "directions":["Step 1.","Step 2.","Step 3.","Step 4.","Step 5."],
    "backgroundImage":"images/Chinese/AbaloneEggCustard.jpg",
    "healthytips":["Tip 1","Tip 2","Tip 3"],
    "nutritions":["Calories 65kcal","Total Fat 2.2g","Carbs 4g","Cholesterol 58mg","Sodium 311mg","Dietary Fibre 0.3g"],
    "ingredients":["1 head Napa or bok choy cabbage","1/4 cup sugar","1/4 teaspoon salt","3 tablespoons white vinegar","3 green onions","1 (3-ounce) package ramen noodles with seasoning pack","1 (6-ounce) package slivered almonds","1 tablespoon sesame seeds","1/2 cup vegetable oil"]}
]

我将如何将其保存在数据库中?导致一天结束时我必须从数据库中读取并能够使用 webapi 解析它

【问题讨论】:

    标签: json visual-studio-2010 asp.net-web-api


    【解决方案1】:

    在长度可能超过 varchar 限制的情况下,将其作为CLOB 数据类型保存在您的数据库中。

    【讨论】:

      【解决方案2】:

      这里有很多潜在的答案 - 您需要提供更多详细信息才能获得具体答案。

      数据库
      您使用的是什么数据库——它是关系、对象还是 no-sql?如果您从无 sql 的角度来看 - 将其保存为一个整体可能很好。从 RDBMS 的角度来看(如 SQL Server),您将所有字段映射到一组相关表中的一系列行。如果您使用的是关系数据库,那么仅仅将未解析、未验证的 JSON 文本块塞入数据库中是错误的方式。何苦雇用提供 DRI 的数据库呢。

      数据操作层
      您的问题包括您将使用哪种类型的数据操作 - 可以是 linq to sql,可以是直接 ADO,像 Dapper、Massive 或 PetaPoco 这样的微型 ORM,像 Entity Framework 或 NHibernate 这样的成熟 ORM。

      您是否选择了其中之一,或者您正在寻找有关选择的指导?

      在 WebAPI 中解析
      在 WebApi 中,从 JSON 转换为 Object 或从 Object 转换为 JSON 很容易。特别是对于 JSON,JSON.Net 格式化程序就在附近。您可以通过查看 herehereherehere 开始。

      然而,从概念上讲,您似乎错过了 WebAPI 的魔力。使用 WebAPI,您可以以本机状态返回对象(如果需要 OData 支持,则返回 IQueryable)。在您的函数调用完成后,Formatter 接管并根据客户端请求将其序列化为适当的形状。这个过程称为内容协商。这个想法是您的方法与格式无关,并且框架将数据序列化为您的客户想要的传输格式(xml、json 等)。

      反之亦然,框架将客户端提供的格式反序列化为原生对象。

      【讨论】:

      • @forsakenedsj 这是一个问答网站,人们可以在这里贡献他们的空闲时间来帮助他人。获得好的答案的最佳方式是向志愿者展示您已经付出了一些努力。请参阅:stackoverflow.com/questions/how-to-ask
      猜你喜欢
      • 2014-02-28
      • 2021-02-22
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      相关资源
      最近更新 更多