0

My Json code are as below

[
    {"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"]}
]

how am I going to persist this in database? Cause the end of the day I have to read from the database and able to parse it using webapi

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
forsakenedsj
  • 61
  • 1
  • 6

2 Answers2

0

Persist it as a CLOB data type in your database in the likely event that the length is going to exceed the limits of a varchar.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
0

There are so many potential answers here -- you'll need to provide many more details to get a specific answer.

Database
What database are you using -- is it relation, object, no-sql? If you come from a no-sql perspective -- saving it as a lump is likely fine. From a RDBMS perspective (like SQL Server), you map all the fields down to a series of rows in a set of related tables. If you're using a relation database, just jamming an unparsed, unvalidated lump of JSON text in the database is the wrong way to go. Why bother hiring a database that provides DRI at all.

Data Manipulation Layer
Included in your question is what type of data manipulation you'll use -- could be linq to sql, could be straight ADO, a micro ORM like Dapper, Massive, or PetaPoco, a full blown ORM like Entity Framework or NHibernate.

Have you picked one of these or are you looking for guidance on selecting one?

Parsing in WebAPI
Convering from JSON to an Object or an Object to JSON is easy in WebApi. For JSON specifically, the JSON.Net formatter is hanging around. You can get started by looking here, here, here, and here.

Conceptually, however, it sounds like you're missing part of the magic of WebAPI. With WebAPI you return your object in it's native state (or IQueryable if you want OData support). After your function call finishes the Formatter's take over and serialize it into the proper shape based on the client request. This process is called Content Negotiation. The idea is that your methods are format agnostic and the framework serializes the data into the transport format your client wants (xml,json, whatever).

The reverse is true too, where the framework deserializes the format provided by the client into a native object.

Community
  • 1
  • 1
EBarr
  • 11,826
  • 7
  • 63
  • 85
  • @forsakenedsj This is a Q&A site where people donate their free time to help others. The best way to get good answers is to show the volunteers that you've put some effort in. See: http://stackoverflow.com/questions/how-to-ask – EBarr Aug 10 '12 at 13:09