標籤

2013年7月4日 星期四

使用 Json 格式取得農委會 OpenData

Json
JSON(JavaScript Object Notation)是一種輕量級的資料交換語言,以文字為基礎,且易於讓人閱讀。儘管JSON是Javascript的一個子集,但JSON是獨立於語言的文字格式,並且採用了類似於C語言家族的一些習慣。
wiki json 介紹

安裝方式
使用VS提供的NuGet,搜尋Json.net即可自動安裝。
1. 安裝NuGet Packager

nuGet
2. 開啟 NuGet 管理工具

管理
3. 搜尋Json.NET安裝

jsonPackage

C#範例程式
農委會資料開放平台
nowehou
農委會提供了許多開放資料,主要以Json格式輸出,可使用特定網址來索取資料,以下範例為選擇關鍵字和產品種類,進行搜尋,點選產品之後,再顯示更進一步詳細資料。
JasonOpenData

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace WFA_JSON{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 儲存Json資料之陣列
        /// </summary>
        List<CASData> _listCasData = new List<CASData>();

        /// <summary>
        /// Json格式,參考PDF說明文件設計
        /// </summary>
        public class CASData
        {
            public string no { get; set; }
            public string name { get; set; }
            public string address { get; set; }
            public string phone { get; set; }
            public string fax { get; set; }
            public string manager { get; set; }
            public string web { get; set; }
            public string pno { get; set; }
            public string type { get; set; }
            public string product { get; set; }
            public string intro { get; set; }
        }

        /// <summary>
        /// 組合農委會查詢資料網址
        /// </summary>
        /// <returns></returns>
        private string GetQueryURL()
        {
            string FixURL = "http://data.coa.gov.tw/od/data/api/eir06?$format=json&$filter=";
            string Type="";
            string Product = "";
            bool First = true;
            for (int i = 0; i < checkedListBox1.Items.Count - 1; i++)
            {
                if (checkedListBox1.CheckedItems.IndexOf(checkedListBox1.Items[i]) >= 0)
                {
                    if (First)
                    {
                        Type += "type + like + " + (i + 4).ToString();
                        First = false;
                    }
                    else
                    {
                        Type += "And type + like + " + (i + 4).ToString();
                    }
                }
            }
            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                if (First)
                {
                    Product = "product + like " + textBox1.Text;
                }
                else
                {
                    Product = "And product + like " + textBox1.Text;
                }
            }
            return FixURL + Type + Product;
        }

        /// <summary>
        /// 送出查詢資料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string QueryURL = GetQueryURL();
            StreamReader myFile =                   
            new StreamReader(WebRequest.Create(QueryURL).GetResponse().GetResponseStream());
        
            string myString = myFile.ReadToEnd();

            _listCasData.Clear();
            _listCasData = JsonConvert.DeserializeObject<List<CASData>>(myString);

            listBox1.Items.Clear();
            foreach (CASData data in _listCasData)
            {            
                {
                    listBox1.Items.Add(data.product);
                }
            }                
        }

        /// <summary>
        /// 點選產品名稱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            CASData theData = _listCasData[listBox1.SelectedIndex];
            richTextBox1.AppendText("廠商編號: " + theData.no + "\n");
            richTextBox1.AppendText("廠商名稱: " + theData.name + "\n");
            richTextBox1.AppendText("地址: " + theData.address + "\n");
            richTextBox1.AppendText("電話: " + theData.phone + "\n"); 
            richTextBox1.AppendText("傳真: " + theData.fax + "\n");
            richTextBox1.AppendText("負責人: " + theData.manager + "\n");
            richTextBox1.AppendText("公司網址: " + theData.web + "\n");
            richTextBox1.AppendText("產品編號: " + theData.pno + "\n");
            richTextBox1.AppendText("項目: " + theData.type + "\n");
            richTextBox1.AppendText("產品名稱: " + theData.product + "\n");
            richTextBox1.AppendText("說明: " + theData.intro + "\n");
        }
    }
}

參考網站
[C#]Json.NET - A high performance Json library

沒有留言:

張貼留言