csharp:.net 3.5 using System.Runtime.Serialization.Json read json
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.datacontractattribute.aspx
//using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime;
using System.Runtime.Serialization;// 添加 System.Runtime.Serialization
using System.Runtime.Serialization.Json;//添加 System.ServiceModel和System.ServiceModel.Web的引用
using System.Xml;
using System.Runtime.Serialization.Formatters; namespace JsonNetDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
string dataString = string.Empty; /// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{ }
/// <summary>
/// 对象转化为JSON字符串
/// http://www.cnblogs.com/coderzh/archive/2008/11/25/1340862.html
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
var config = new JsonConfig()
{
encoding = "UTF-8",
plugins = new string[] { "Java", "C++", "C#" },
indent = new Indent() { length = 4, use_space = false }
}; var serializer = new DataContractJsonSerializer(typeof(JsonConfig));
var stream = new MemoryStream();
serializer.WriteObject(stream, config); byte[] dataBytes = new byte[stream.Length]; stream.Position = 0; stream.Read(dataBytes, 0, (int)stream.Length); dataString = Encoding.UTF8.GetString(dataBytes); Response.Write("JSON string is:");
Response.Write(dataString);
}
/// <summary>
/// JSON字符串转对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
var config = new JsonConfig()
{
encoding = "UTF-8",
plugins = new string[] { "Java", "C++", "C#" },
indent = new Indent() { length = 4, use_space = false }
}; var serializer = new DataContractJsonSerializer(typeof(JsonConfig));
var stream = new MemoryStream();
serializer.WriteObject(stream, config); byte[] dataBytes = new byte[stream.Length]; stream.Position = 0; stream.Read(dataBytes, 0, (int)stream.Length); dataString = Encoding.UTF8.GetString(dataBytes); //var serializer = new DataContractJsonSerializer(typeof(JsonConfig));
var mStream = new MemoryStream(Encoding.Default.GetBytes(dataString));
JsonConfig readConfig = (JsonConfig)serializer.ReadObject(mStream); Response.Write(string.Format("Encoding is: {0}", readConfig.encoding));
foreach (string plugin in readConfig.plugins)
{
Response.Write(string.Format("plugins is: {0}", plugin));
}
Response.Write(string.Format("indent.length is: {0}", readConfig.indent.length));
Response.Write(string.Format("indent.use_space is: {0}", readConfig.indent.use_space));
}
} [DataContract(Name = "Customer", Namespace = "JsonNetDemo")]
class JsonConfig
{
[DataMember(Order = 0)]
public string encoding { get; set; }
[DataMember(Order = 1)]
public string[] plugins { get; set; }
[DataMember(Order = 2)]
public Indent indent { get; set; }
} [DataContract(Name = "Customer", Namespace = "JsonNetDemo")]
class Indent
{
[DataMember(Order = 0)]
public int length { get; set; }
[DataMember(Order = 1)]
public bool use_space { get; set; }
}
}
csharp:.net 3.5 using System.Runtime.Serialization.Json read json的更多相关文章
- C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSerializer.Serialize
In .net core 3.0 using System;using System.Collections.Generic;using System.Collections;using System ...
- 找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”的解决办法
找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”.的解决办法站点发布后部署到了两 ...
- 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则
在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...
- 找不到System.Runtime.Serialization.Json的解决方案
System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用
- 引用System.Runtime.Serialization.Json
vs2012下,重新添加一次System.Runtime.Serialization的引用
- 基础命名空间:序列化_自定义序列化 System.Runtime.Serialization
( (From Msdn) 自定义序列化是控制类型的序列化和反序列化的过程,通过控制序列化,可以确保序列化兼容性.换而言之,在不中断类型核心功能的情况下,可在类型的不同版本之间序列化和反序列化. 重 ...
- 基础命名空间:序列化 System.Runtime.Serialization
对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化. ...
- System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生
异常信息: “System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization. ...
- System.Runtime.Serialization.cs
ylbtech-System.Runtime.Serialization.cs 允许对象控制其自己的序列化和反序列化过程. 1.返回顶部 1. #region 程序集 mscorlib, Versio ...
随机推荐
- JSON与XML比较
1. 定义介绍 1.1 XML定义 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种 ...
- Linux rsync 企业级应用
简介 rsync 是 Linux 下的数据同步工具, 其支持本地同步和远程同步, 远程同步分为 daemon 和 ssh 同步方式 rsync 可以代替 cp, scp 等命令, 且具有更高的可 ...
- Supreme(ง •̀_•́)ง
1.数学: 4月底18讲看完. 5月书上重点习题再过一遍(注证明题,最值,平均值,有界,介值,零点定理,等式不等式,极限,中值定理(费马,罗尔,拉格朗日,泰勒,柯西,一元积分中值定理,二重积分中值定理 ...
- golang (2) package
综述 golang package是基本的管理单元,package是golang最基本的分发单位和工程管理中依赖关系的体现. 每个golang源代码文件开头都拥有一个package声明,表示该gola ...
- @PostConstruct和@PreConstruct注解
@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明. @PostConstruct //方式1 public v ...
- SpringCloud与Docker微服务架构实战笔记
一 微服务架构概述 1. 单体应用架构存在的问题 结合:https://www.cnblogs.com/jialanshun/p/10637454.html一起看,在该篇博客中搜索“单块架构的优缺点 ...
- C++ 流控制函数setw()、setfill()、setbase()、setprecision()的使用
头文件: #include <iostream> #include <iomanip> 功能: std::setw :需要填充多少个字符,默认填充的字符为' '空格 std:: ...
- 安装Telerik JustMock插件后启动不成功
1.打开Telerik JustMock Configuration 勾选所有框 2.到C:\Program Files (x86)\Progress\Telerik JustMock\Librari ...
- $bzoj1014-JSOI2008$ 火星人$prefix$ $splay$ $hash$
题面描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:\(madamimadam\),我们将这个字符串的各个字符予以标号: 序号 1 2 3 4 5 6 7 8 ...
- AR和VR的区别到底在哪?
AR是Augmented Reality的字母缩写,中文名字是“增强现实”,是一种全新人机交互技术.通过AR技术,让参与者与虚拟对象进行实时互动,从而获得一种奇妙的视觉体验,而且能够突破空间.时间以及 ...