Rabu, 25 Desember 2013

Parsing Json di Windows Phone dengan C#

Pada kesempatan kali ini kita akan bahas mengenai parsing JSON json adalah sebuah teks string hasil dari direct get atau post sebuah API ...

langsung aja cekidot ...

1. buat project ....

2. download newtonsoft.json atau bisa di donlot di mari
Json  dan masukkan di reference

klik kanan di reference -> add -> browse  -> masukkan file yang di donlot tadi ..

sip ...
buat class get; set; sperti ini
jika sulit atau tidak tau bisa buka link ini json2csharp
masukkin link APInya ...
ntar copas deh ...
begitu mudahnya ....
3. buat class download string async buat API...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PhoneApp1
{
    class download
    {
        public static getset getset;
        public void downlaoad() 
        {
            WebClient web_Task1 = new WebClient();
            web_Task1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringjson);
            web_Task1.DownloadStringAsync(new Uri("...LINK API AGAN..."));
        
        }

        private void proxy_DownloadStringjson(object sender, DownloadStringCompletedEventArgs e)
        {
            getset = JsonConvert.DeserializeObject<getset>(e.Result);
        }
    }
}

buatlah classinit.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhoneApp1
{
    class Classinit
    {
        private static download Download = null;

        public static download _doWnload
        {
            get
            {
                // Delay creation of the view model until necessary
                if (_doWnload == null)
                    _doWnload = new download;

                return _doWnload;
            }
        }
    }

lalu panggil di mainpage.xaml.cs

 public MainPage()
        {
            InitializeComponent();
            ClassINit._doWnload.downlaoad;

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }


cukup mudah kan ...

terima kasih atas attensinya ...

sampai jumpa lagi...






0 komentar:

Posting Komentar