ASP.NET Core 101 - Adding Data to an ASP.NET Core Website [3 of 13]

What is a database?
Database is where you store all that information that you're going to go and retrieve from it.
It could be a static database,which doesn't change very often. It could be a SQL Server.
You here things like that, or SQLite.We also can use a file as a database.

So I have one on my desktop called products.json. that's JavaScript Object Notation.

复制代码
[
  {
    "Id": "jenlooper-cactus",
    "Maker": "@jenlooper",
    "img": "https://user-images.githubusercontent.com/41929050/61567048-13938600-aa33-11e9-9cfd-712191013192.jpeg",
    "Url": "https://www.hackster.io/agent-hawking-1/the-quantified-cactus-an-easy-plant-soil-moisture-sensor-e65393",
    "Title": "The Quantified Cactus: An Easy Plant Soil Moisture Sensor",
    "Description": "This project is a good learning project to get comfortable with soldering and programming an Arduino.",
    "Ratings": [
      5,
      5
    ]
  },
  {
    "Id": "jenlooper-light",
    "Maker": "jenlooper",
    "img": "https://user-images.githubusercontent.com/41929050/61567049-13938600-aa33-11e9-9c69-a4184bf8e524.jpeg",
    "Url": "https://www.hackster.io/agent-hawking-1/book-light-dee7e4",
    "Title": "A beautiful switch-on book light",
    "Description": "Use craft items you have around the house, plus two LEDs and a LilyPad battery holder, to create a useful book light for reading in the dark.",
    "Ratings": null
  }
]
复制代码

And then convert it into the kind of object that we want to see.I'm going to right-click and "Add" a new class.

Notice this is interesting because the folder was called Models,I ended up getting the three name space there.

New Vertical Tab

Now, one thing I could do is I could actually right-click on this tab.

 You see this tabs that show all of our different files? I can right-click it and I could say New Vertical Tab.

prop

if you try typing in "Prop".You should geta nifty(有技巧的; 精确的; 实用的; 灵便的;) the template. Code Snippet for property.

 So prop, Tab, Tab, 'property type', Tab,Tab, 'property name', Enter, Enter,

 

 JSON Property

JSON Property,which is an attribute that you can stick(将…刺入(或插入); 刺; 戳; 插入; 粘贴;) at the top of your property,and then map that way.

复制代码
using System.Text.Json;
namespace ContosoCrafts.WebSite.Models
{
    public class Product
    { 
        [JsonPropertyName("img")] 
        public string Image { get; set; }    
    }
}
复制代码

So I'm going to say that the JSON property name is img. What we're doing is we're mapping img image.

Cool. Yes, because I didn't want to abbreviate(缩略; 把(词语、名称)缩写(成…);) the img.Yes. I don't want the shape of my database to affect maybe the shape of my objects.

ToString

Every object or most objects in .net have a ToString. And it gives you a nice way to look at that object as a String.
ToString, ToString is actually a thing that objects already have.So we have to go and make a public one that is an override,because objects get a ToString for free,and we're doing our own.

So that basically means whatever the default was for that ToString method,we're saying just ignore that, here's our own version of the ToString method that we're going to use.

 what if you wanted to convert all this information back into the original JSON that we want? I would want a JSON representation of a product.

So let's go and do that,and I can do it two ways.
First, I could type in like ID plus, Maker plus, I could use a StringBuilder.
Second,JsonSerializer.
We're going to serialize it. What does serialize mean?

So serialize is you're taking all of this object information,and you are converting it back into the text that will be part of the JSON file.And serial means to do
something one after the other.

So to serialize is to go and take product and product and product and all the properties and the properties,and one after the other make a nice String.

复制代码
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ContosoCrafts.WebSite.Models
{
    public class Product
    {
        public string Id { get; set; }
        public string Maker { get; set; }
        
        [JsonPropertyName("img")]
        public string Image { get; set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int[] Ratings { get; set; }

        public int MyProperty { get; set; }

        public override string ToString() => JsonSerializer.Serialize<Product>(this);
    }
}
复制代码

 

posted @   FH1004322  阅读(217)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示