博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Newtonsoft.Json 应用
阅读量:7246 次
发布时间:2019-06-29

本文共 2981 字,大约阅读时间需要 9 分钟。

Newtonsoft.Json下载地址

根据自己的项目生成DLL并引用

JSON格式为

{"result":"200","info":[{"_cid":5,"_name":"Iphone4","_parentid":2,"_status":true},{"_cid":7,"_name":"手机外套","_parentid":2,"_status":true},{"_cid":8,"_name":"手机模","_parentid":2,"_status":true}]} 解析JOSN
View Code
Maticsoft.Model.BackInfo backinfo = (Maticsoft.Model.BackInfo)                 JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackInfo));              if (backinfo.result == "200")//成功             {                 List
list = backinfo.info; Dictionary
dic = new System.Collections.Generic.Dictionary
(); for (int i = 0; i < list.Count; i++) { Maticsoft.Model.IphoneClassify classify = (Maticsoft.Model.IphoneClassify)list[i]; dic.Add(classify._cid, classify._name); } this.DataList1.DataSource = dic; this.DataList1.DataBind(); } else { //失败 }

这里特别注意的是 JSON的字段要和Model类的属性名保持一至不然返回Null 就是这个问题研究半天才弄明白,多谢和

的帮忙

我的model类是用动软生成 的

最后将model类改为 

View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Maticsoft.Model{    ///     ///返回JSON属性    ///     [Serializable]   public class BackInfo    {        public BackInfo() {                   }        public String result { get; set; }        public List
info { get; set; } } ///
///产品类型 /// [Serializable] public class IphoneClassify { public int _cid { get; set; } public string _name { get; set; } public int _parentid { get; set; } public bool _status { get; set; } }}

 

序列化JOSN
View Code
DataTable dt = new Maticsoft.BLL.Iphone_Classify().GetList("parentID=" + strParentID).Tables[0];                if (dt.Rows.Count > 0)                {                    List
list = new List
(); foreach (DataRow row in dt.Rows) { Maticsoft.Model.Iphone_Classify model = new Model.Iphone_Classify() { CID = int.Parse(row["CID"].ToString()), Name = row["Name"].ToString(), parentID = int.Parse(row["parentID"].ToString()), Status = bool.Parse(row["Status"].ToString()) }; list.Add(model); } //微软的JSON序列化 // Classifyinfo = Json.JsonInfo("200", Json.CompanyJsonSerializer
>(list)); // Newtonsoft.Json Classifyinfo = Json.JsonInfo("200", JsonConvert.SerializeObject(list)); } else { Classifyinfo = Json.JsonInfo("400", "未找到相应数据"); }

 

 

转载地址:http://qejbm.baihongyu.com/

你可能感兴趣的文章
XSS原理及防范?
查看>>
必看!互联网开发模式的经验之谈
查看>>
mybatis异常 :元素内容必须由格式正确的字符数据或标记组成。
查看>>
docker中启动关闭删除所有的容器命令
查看>>
python语言基础之正则表达式2,随机数
查看>>
【界面专访】李强:SAP「中国加速计划」落地生根
查看>>
【阿里云总监课第四期】时髦的云原生应用怎么写?
查看>>
白话TCP为什么需要进行三次握手
查看>>
三个开源硬件项目
查看>>
Have fun with Treasure Trails %enjoy 8% off cheap
查看>>
pdf转word如何转?最简单的方法你知道吗?
查看>>
“VR女友”制作人访谈
查看>>
阿列克谢·卡什巴斯基:有机生命渲染
查看>>
一文了解 Apache Flink 核心技术
查看>>
科略教育—管理者应具备五大能力
查看>>
mac上使用dex2jar遇到的权限问题的解决
查看>>
我的友情链接
查看>>
定位于地图小程序
查看>>
学习go语言 我的习题答案 chapter3
查看>>
vCenter Server Appliance 6.5 中重置丢失或忘记的 root 密码
查看>>