新聞中心
本文著重介紹下JSON數(shù)據(jù)格式的相關(guān)內(nèi)容說(shuō)明,隨著JSON的不斷發(fā)展,編程的技巧也出現(xiàn)了不一樣的形式,對(duì)一些輕量級(jí)的數(shù)據(jù)交換格式進(jìn)行編寫(xiě)時(shí),需要注意一些問(wèn)題,比如輕量級(jí)的數(shù)據(jù)交換格式的問(wèn)題。#t#

溫江網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,溫江網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為溫江上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的溫江做網(wǎng)站的公司定做!
本次工作內(nèi)容是要將以下數(shù)據(jù)解析成.Net可以使用的數(shù)據(jù),返回的數(shù)據(jù)除了header,其他的都是可變的,也就是說(shuō)結(jié)構(gòu)不是固定的。完全由用戶(hù)選擇,所以選擇了生成DataTable。
SON數(shù)據(jù)格式如下:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Web.Script.Serialization;
- namespace Tencent.Itil.Cmsi.Common
- {
- public class GeneralSearchResult
- {
- public Header header = new Header();
- private DataTable fieldDefine = new DataTable();
- ///
- /// 返回的數(shù)據(jù)結(jié)構(gòu)定義,無(wú)數(shù)據(jù)
- ///
- public DataTable FieldDefine
- {
- get { return fieldDefine; }
- set { fieldDefine = value; }
- }
- private DataTable retrunData = new DataTable();
- ///
- /// 返回的數(shù)據(jù),格式為DataTable,結(jié)構(gòu)和FieldDefine中的結(jié)構(gòu)一樣
- ///
- public DataTable RetrunData
- {
- get { return retrunData; }
- set { retrunData = value; }
- }
- ///
- /// 將json數(shù)據(jù)轉(zhuǎn)換為定義好的對(duì)象,數(shù)據(jù)轉(zhuǎn)換為DataTable
- ///
- /// name="jsonText">
- ///
- public static GeneralSearchResult GetTransformData(string jsonText)
- {
- GeneralSearchResult gsr = new GeneralSearchResult();
- JavaScriptSerializer s = new JavaScriptSerializer();
- Dictionary
, object> JsonData = (Dictionary , object>)s.DeserializeObject(jsonText); - Dictionary
, object> dataSet = (Dictionary , object>)JsonData["dataSet"]; - Dictionary
, object> header = (Dictionary , object>)dataSet["header"]; - Dictionary
, object> fieldDefine = (Dictionary , object>)dataSet["header"]; - Dictionary
, object> data = (Dictionary , object>)dataSet["data"]; - object[] rows = (object[])data["row"];
- gsr.header.Version = header["version"].ToString();
- gsr.header.ErrorInfo = header["errorInfo"].ToString();
- gsr.header.ReturnCode = header["returnCode"].ToString();
- gsr.header.ReturnRows = Convert.ToInt16(header["returnRows"]);
- gsr.header.TotalRows = Convert.ToInt16(header["totalRows"]);
- Dictionary
, object> dicFieldDefine = (Dictionary , object>)dataSet["fieldDefine"]; - foreach (KeyValuePair
, object> ss in dicFieldDefine) - {
- gsr.FieldDefine.Columns.Add(ss.Key, typeof(string));
- }
JSON數(shù)據(jù)格式使用方法:
- GeneralSearchResult gsr = new GeneralSearchResult();
- gsr = GeneralSearchResult.GetTransformData(text);
網(wǎng)頁(yè)名稱(chēng):大致闡述JSON數(shù)據(jù)格式的編寫(xiě)與運(yùn)行方式
文章URL:http://fisionsoft.com.cn/article/cdceeds.html


咨詢(xún)
建站咨詢(xún)
