JSON Tutorial - Dzidza Mashandisiro Ekushandisa JSON neJavaScript

Muchidzidzo ichi, tichaenda kudzidza nezve JSON. Tichavhara chimiro cheJSON, mhando dzakasiyana dzedata uye maitiro ekushandisa JSON mukati meJavaScript.

JSON imwe yemafungiro akakosha iwe aunogona kudzidza seanogadzira kana seQA.

Pese paunoronga basa raunenge uchizoshandisa JSON nguva dzese kunyangwe iri kugadzira API, kushandisa API, kana kugadzira mafaira ekumisikidza yako application.

Chii chinonzi JSON

JSON iyo inomiririra JavaScript chinhu notation, ingori dhata inomiririra fomati yakafanana chaizvo neXML kana YAML.

Inoshandiswa zvakanyanya kune iyo internet kune angangoita ese ega API ayo iwe aunowana, pamwe nekugadzirisa mafaira uye zvinhu zvakaita semitambo uye mameseji edhita.

Muenzaniso weJSON:



#user.json {
'name': 'Steve',
'age': 43,
'isProgrammer' true,
'hobbies': ['Reading Java books', 'cooking', 'classic music'],
'friends': [{
'name': 'joey',
'age': 39,
'isProgrammer': false,
'friends': [...]
}] }

Nei Uchishandisa JSON

Isu tinoshandisa JSON nekuti inoreruka zvakanyanya kutumira kumashure nekudzoka mu http zvikumbiro nemhinduro nekuda kweiyo diki saizi saizi.

Zviri nyore kuverenga zvichienzaniswa nechimwe chinhu senge XML sezvo iri yakachena zvakanyanya uye hapana akawanda seanovhura nekuvhara ma tag kunetseka nezvazvo.

JSON zvakare inosangana zvakanaka kwazvo neJavaScript sezvo JSON ichingova chidimbu cheJavaScript, zvinoreva kuti chero chinhu iwe chaunonyora muJSON chiri JavaScript inoshanda.

Anenge mitauro yega yega mikuru ine imwe nzira yeraibhurari kana yekuvakira-mukati mashandiro ekutsikisa tambo dzeJSON muzvinhu kana makirasi mumutauro iwoyo.

Izvi zvinoita kushanda neJSON dhata zvakanyanya nyore mukati merurimi rwechirongwa.

JSON Dhata Dhata

Iye zvino zvatiri kunzwisisa kuti chii chinonzi JSON uye nei chakakosha, ngatinyurire mune imwe syntax inobatanidzwa uye nemhando dzedata dzinogona kumiririrwa naJSON.

Sezvo isu tichiziva JSON iri data inomiririra fomati saka isu tinofanirwa kukwanisa kumiririra imwe dhata mhando mukati mayo.

JSON natively inotsigira:

  • tambo
  • nhamba manhamba anogona kuve mune chero fomati kunyangwe ari manhamba ekuverengera manhamba ese manhamba asina kunaka nyangwe nhamba dzesainzi notation
  • booleans izvo zvinogona kuve zvechokwadi kana nhema
  • null izvo zvinomiririra pasina
  • Arrays iro rinogona kuve runyorwa rwemhando dzipi dziri pamusoro
  • Zvinhu chinhu chakanyanya kuomarara asi chinonyanya kushandiswa mhando mukati mejson sezvo ichitendera iwe kuti umiririre data ari akakosha akakosha mapara

JSON Muenzaniso

Ngatinyurirei mumuenzaniso mashandisiro json mukati mefaira.

Chinhu chekutanga chaunofanirwa kuita kugadzira faira ne .json kuwedzera pamagumo acho.

Tichagadzira user.json faira ine chinhu chemushandisi chinomiririrwa seJSON.

Kugadzira chinhu isu tinofanirwa kushandisa kuvhura nekuvhara makona akasungirirwa {} uyezve mukati meizvozvo isu tichaisa ese akakosha kukosha mapara anoita chinhu chedu.

Chero chinhu chimwe chete mukati meJSON chinhu chakakosha kukosha. Iyo kiyi inofanirwa kukomberedzwa nekaviri '' makotesheni anoteverwa nekoloni : uyezve kukosha kweiyi kiyi.

Kana isu tine akawanda akakosha kukosha mapara, isu tinoda makoma , kupatsanura yega yega yega yedu yakakosha mapara maviri, zvakafanana nemagadzirisiro atingaita rondedzero mune yakajairwa programming mutauro.

Semuenzaniso JSON Faira

#user.json {
'name': 'Steve',
'age': 43,
'isProgrammer' true,
'hobbies': ['Reading Java books', 'cooking', 'classic music'],
'friends': [{
'name': 'joey',
'age': 39,
'isProgrammer': false,
'friends': [...]
}] }

Mumuenzaniso uri pamusoro, isu tine faira rakanzi user.json. Mukati mefaira tine mhando dzakasiyana dzedata.

Makiyi anogara akakomberedzwa nemaviri makotesheni. Pamitengo, chete tambo mhando yakakomberedzwa nemaviri makotesheni.

Mumuenzaniso:

  • zita ndi string
  • zera | | + _ |
  • isProgrammer iri integer
  • zvinokuvaraidza i boolean
  • shamwari ndeye Array
Ongorora:Iko hakuna comma pakupera kwekupedzisira chivakwa muJSON faira kana chinhu cheJSON.

Mashandisiro Ekushandisa JSON Tambo Mukati meJavaScript

Ngatifungei kuti tine faira reJSON rakanzi Array of Objects irwo rondedzero yezvinhu zvekambani:

companies.json

Mumuenzaniso wepamusoro, isu tine zvinhu zviviri zvekambani mukati meiyo JSON array.

Zvino ngatione mashandisiro atingaite iri pamusoro JSON mukati meJavaScript.

Muzviitiko zvakawanda, tinowana JSON setambo pane kuita chinhu cheJSON. Kutevedzera izvi, isu tinomiririra pamusoro apa JSON setambo mukati meJavaScript.

Yedu html faira inoita senge:

[
{
'name': 'Big corporate',
'numberOfEmployees': 1000,
'ceo': 'Neil',
'rating': 3.6
},
{
'name': 'Small startup',
'numberOfEmployees': 10,
'ceo': null,
'rating': 4.3
} ]

Kana isu tichiongorora iyo danda rekunyaradza muChannel Yekuvandudza Maturu, zvinobuda zvakafanana nezvakaratidzwa pazasi:

JSON muenzaniso JavaScript

Ipapo isu tinokwanisa kuenzanisa iri pamusoro JSON nekutsanangudza zvatiri kuda kutora. Semuenzaniso, kana isu taida kuwana zita rekambani yekutanga mune yakarongeka yatinoshandisa:


JSON Example

let companies =
`[
{

'name': 'Big corporate',

'numberOfEmployees': 1000,

'ceo': 'Neil',

'rating': 3.6
},
{

'name': 'Small startup',

'numberOfEmployees': 10,

'ceo': null,

'rating': 4.3
}
]`
console.log(JSON.parse(companies))

Saizvozvo, kuwana chiyero chekambani yechipiri yatinoshandisa:

console.log(JSON.parse(
companies[0].name )) Output: Big corporate

Maitiro ekushandura JavaScript Chinhu kuva JSON

Zvino ngatiti tine chinhu cheJavaScript senge chiri kuratidzwa pazasi:

console.log(JSON.parse(
companies[1].rating )) Output: 4.3

Kuchinja munhu JavaScript chinhu kuva JSON tinoshandisa
JSON Example

var person = {
name: 'Brad',
age: 35
}
nzira:

stringify

Zvinobuda ndezvechokwadi JSON:

jsonPerson = JSON.stringify(person); Ongorora:{
'name': 'Brad',
'age': 35 }
anodhindisa Chirungu . Kuti titore kukosha, isu tinofanirwa kushandura iyo JSON kudzokera kuJavaScript chinhu.

Kuti tiite riri pamusoro basa, tinofanirwa kushandura iyo JSON kudzokera kuJavaScript chinhu.

Ungashandura sei JSON Chinhu kuJavaScript

Kuti tishandure chinhu chiri pamusoro cheJSON kudzokera kuJavaScript, tinoshandisa console.log(jsonPerson.name) nzira:

parseOngorora:Zvino kana isu takaedza jsPerson = JSON.parse(jsonPerson) tinowana 'Brad'.

Wakazara Muenzaniso

console.log(jsPerson.name)

Pfupiso

  • JSON inomiririra JavaScript Object Notation
  • Yakareruka dhata-yekuchinjana fomati
  • Kubva pane subset yeJavaScript
  • Zviri nyore kuverenga nekunyora
  • Mutauro wakazvimirira
  • Inogona kuiswa mumitauro mizhinji yemazuva ano ekuronga

Dhata Dhata:

  • Nhamba: Hapana mutsauko pakati pehuwandu uye kuyangarara
  • Tambo: Tambo ye Unicode mavara. Shandisa zvakapetwa kaviri
  • Boolean: Chokwadi kana nhema
  • Array: Yakarongedzwa runyorwa rwe0 kana mamwe maitiro mu
    JSON Example

    var person = {
    name: 'Brad',
    age: 35
    }
    jsonPerson = JSON.stringify(person); //convert to JSON
    console.log(jsonPerson.name); //undefined
    jsPerson = JSON.parse(jsonPerson); //convert to JS Object
    console.log(jsPerson.name); //Brad
  • Chinhu: Unordered kuunganidzwa kwekiyi / kukosha mapara
  • Null: Kukosha kusina chinhu

JSON Syntax Mitemo:

  • Inoshandisa kiyi / kukosha mapara - i.e. []
  • Inoshandisa zvakapetwa zvakapoteredzwa CHINOKOSHA
  • Inofanira kushandisa yakatarwa mhando dzedata
  • Rudzi rwefaira iri .json
  • MIME mhando ndeye 'Kunyorera / json'

Ndinovimba wawana iyi dzidziso yeJSON neJavascript inobatsira. Iwe unogona ikozvino kunyora zvakapusa uye zvakaoma mafaira eJSON uye nekudyidzana neJSON tambo mukati meJavaScript.