Yan 17, 2017 Dasturlash

JSON va uning qo’llanishi

JSON (JavaScript Object Notation) bu malumatlar almashinishi uchun ishlatiladigan text formati bo’lib, javascript uchun yaratilgan va aynan shu tilda ko’p qo’llaniladi.
Duglas Krokford tomonidan yaratilgan. JSON javascript uchun yaratilganiga qaramasdan uni ko’pchilik tillarda ishlatishimiz mumkin. Aynam men uni androidda server bilan malumot almashganda ishlatganman.
Server json ko’rinishida malumotlarni jo’natgan, abdroidda u parse qilinib mos class obyektlariga aylantirilgan. Shaxsiy tajribamdan ( uncha katta emas-u 🙂 )
kelib chiqib aytadigan bo’lsam, JSON obyekt asason frontendga backenddan malumotlarni jo’natishda ishlatiladi.
Hususan PHPga keladigan bo’lsak uning 5.2 versiyasidan boshlab json_encode() va json_decode() junksiyalari kiritilgan.
JSON ga misol:

<code><span class="pln">
</span><span class="pun">{</span><span class="pln">
   </span><span class="str">"firstName"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Иван"</span><span class="pun">,</span><span class="pln">
   </span><span class="str">"lastName"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Иванов"</span><span class="pun">,</span><span class="pln">
   </span><span class="str">"address"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
       </span><span class="str">"streetAddress"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Московское ш., 101, кв.101"</span><span class="pun">,</span><span class="pln">
       </span><span class="str">"city"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Ленинград"</span><span class="pun">,</span><span class="pln">
       </span><span class="str">"postalCode"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">101101</span><span class="pln">
   </span><span class="pun">},</span><span class="pln">
   </span><span class="str">"phoneNumbers"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
       </span><span class="str">"812 123-1234"</span><span class="pun">,</span><span class="pln">
       </span><span class="str">"916 123-4567"</span><span class="pln">
   </span><span class="pun">]</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span>

Ko’rib turganingizdek u 2 qismdan iborat: key(o’zgaruvchi nomi) va

qiymati. Nomidan kelib chiqib chiqqan holda u oddiy javascript obyektga

juda o’xshaydi. Javascript Obyekt haqidagi postni o’qishingiz mumkin. Ularning assosiy farqi:

  • Key (o’zgaruvchi nomi) har doim « ichida yozilishi kerak.
  • Qabul qiladigan qiymatlari
    • string
    • sonli qiymat
    • boshqa JSON obyekt
    • massiv
    • bool qiymat
    • null

Masalan:

<code><span class="pln">
</span><span class="com">// JSON:</span><span class="pln">
</span><span class="pun">{</span><span class="pln"> </span><span class="str">"foo"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"bar"</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

</span><span class="com">// Javascript obyekt:</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> o </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> foo</span><span class="pun">:</span><span class="pln"> </span><span class="str">"bar"</span><span class="pln"> </span><span class="pun">};</span><span class="pln">

</span>

Biz javascript obyektni var o = { foo: «bar» }; deb yozgan taqdirimizda ham javascript xatosiz ishlayveradi.Nimaga endi JSONda key « ichida yoziladi? Tepada aytganimizdek, json o’zaro malumot almashish formati va malumot qabul qiladigan tomon malumotni parse qilishi uchun shunday o’zgarmas qonunlarni joriy qilgan.

 

Balki siz bunday ko’rinishdagi obyektlarni boshqa dasturlash tillarida ishlatgandirsiz.

Turli dasturlash tillarida uni har hil atalgan holatlarini uchratish

mumkin, masala struktura, dictionary, hash jadval, kalit so’zli ro’yxati

yoki assotsiativ

massiv va hakazo.

Manba: Texnoman

Fikr bildirish

Email manzilingiz chop etilmaydi. Majburiy bandlar * bilan belgilangan