C# da json parser!

Har bir dasturchi o’z amaliyoti davomida REST(Representational State Transfer) va SOAP(Simple Object Access Protocol) api serveslari bilan ishlashga tog’ri keladi.
REST va SOAP api servislariga kelaydigan bo’lsak SOAP bu ma’lumotlarni xml ko’rinishda yuboradi. REST esa 3 xil ko’rinishda html, xml, json. O’zimning amalyotim davomida OpenStreetMap bilan ishlashga tog’ri kelgan. Men undan qidiruv uchun foydalangandim:
http://nominatim.openstreetmap.org/search/tashkent?format=json

Hozirgi maqolamizda esa C# da json bilan ishlashning eng oddiy yo’lini ko’rib chiqamiz va kelgusida REST bilan ishlashga bir qadam tashlaymiz.
.Net uchun Codeplexda JSON.NET loyihasi bor va biz uni yuklab olamiz.
VisualStudio ni ochamiz va C# da consol turudagi json deb yangi loyiha yaratamiz.

Loyihamizga json kutubxonamizni Newtonsoft.Json.dll ni add refrence orqaliy qo’shamiz.

Usingda JSON bibliotekalarini va .net ni IO va Net larni ham qo’shamiz:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;


Bizning dasturimiz json ma’lumotni localhostdan olishi kerak.
Denwer orqaliy localhost/json/json.php deb yaratamiz va json.php faylimizga json qiymat beramiz.

{
    "Name": "Dilshod",
    "email": [
        "dasturchiuz@gmail.com",
        "dasturchiuz@mail.ru"
    ],
    "site": {
        "website": "projects.uz",
        "blog": "dasturchi.zn.uz"
    }
}

C# qismiga o’tamiz Program classimizning main metodiga o’tamiz:
WebRequest wquery = WebRequest.Create(@"http://localhost/json/json.php");
WebResponse wrespon = wquery.GetResponse();
Stream wstream = wrespon.GetResponseStream();
StreamReader rd = new StreamReader(wstream);
JObject json = JObject.Parse(rd.ReadToEnd());
Console.WriteLine("Ismi: "+json["Name"]);
Console.WriteLine("Gmail.com: " + json["email"][0]);
Console.WriteLine("Mail.ru: " + json["email"][1]);
Console.WriteLine("Web sayti: " + json["site"]["website"]);
Console.WriteLine("Blogi: " + json["site"]["blog"]);
Console.ReadKey();

Loyihani ishga tushuramiz F5.

Full code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;

namespace json
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest wquery = WebRequest.Create(@"http://localhost/json/json.php");
            WebResponse wrespon = wquery.GetResponse();
            Stream wstream = wrespon.GetResponseStream();
            StreamReader rd = new StreamReader(wstream);
            JObject json = JObject.Parse(rd.ReadToEnd());
            Console.WriteLine("Ismi: "+json["Name"]);
            Console.WriteLine("Gmail.com: " + json["email"][0]);
            Console.WriteLine("Mail.ru: " + json["email"][1]);
            Console.WriteLine("Web sayti: " + json["site"]["website"]);
            Console.WriteLine("Blogi: " + json["site"]["blog"]);
            Console.ReadKey();
        }
    }
}

2 комментария

shranet
SOAP haqida ham yozsangiz yaxshi bo'lardi. Serverdagi PHP funksiyasi chaqirish misollari bilan.
1