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的更多相关文章

  1. 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 ...

  2. 找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”的解决办法

    找不到方法:“Boolean System.Runtime.Serialization.DataContractAttribute.get_IsReference()”.的解决办法站点发布后部署到了两 ...

  3. 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则

    在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObject ...

  4. 找不到System.Runtime.Serialization.Json的解决方案

    System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用

  5. 引用System.Runtime.Serialization.Json

    vs2012下,重新添加一次System.Runtime.Serialization的引用

  6. 基础命名空间:序列化_自定义序列化 System.Runtime.Serialization

    (  (From Msdn) 自定义序列化是控制类型的序列化和反序列化的过程,通过控制序列化,可以确保序列化兼容性.换而言之,在不中断类型核心功能的情况下,可在类型的不同版本之间序列化和反序列化. 重 ...

  7. 基础命名空间:序列化 System.Runtime.Serialization

    对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化. ...

  8. System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生

    异常信息: “System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization. ...

  9. System.Runtime.Serialization.cs

    ylbtech-System.Runtime.Serialization.cs 允许对象控制其自己的序列化和反序列化过程. 1.返回顶部 1. #region 程序集 mscorlib, Versio ...

随机推荐

  1. cap-insets

    原文链接 在很多编程语言中都有resizable image这样的概念,比如android中的NinePatch graphic ,css3中的border image,微软的Nine-Grid Re ...

  2. SQLAlachemy 自动提交配置 SQLALCHEMY_COMMIT_ON_TEARDOWN

    挖坑:自动提交省去了每次 commit,添加数据对象后立马取 id 返回None 填坑 :立马要取 id 的地方 commit一下

  3. Docker安装Odoo实现快速迁移(四)

    一. 安装postgres数据库 root@ubuntu-:~# docker run -d -e POSTGRES_USER=odoo -e POSTGRES_PASSWORD=odoo --nam ...

  4. SAP生产订单各种日期的计算说明

    生产订单各种日期的计算说明 基本日期.已计划的.确认的日期,介绍一下这些日期的作用和计算方法: 首先我们来介绍一下基本日期: 基本开始日期:表示订单的开始日期 基本完成日期:表示订单的完成日期 我们在 ...

  5. linux 搭建gitlab server

    以root 身份进行登录linux 1.先安装相关依赖: sudo yum install -y curl policycoreutils-python openssh-server 2.开启SSH开 ...

  6. vue开发的一些设置以及技巧

    引入其它css样式   需要加~   @找到的是src目录 对于常用的路径可以设置别名     @代表src                  常用的地址在webpack.base.conf.js中 ...

  7. Dijkstra实现最短路径

    #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const ...

  8. cmake 简学

    https://www.cnblogs.com/cv-pr/p/6206921.html

  9. win10操作系统系统,小米路由器,小米3 的问题

    注意 , 置顶 单独一篇 : { win10  局域网共享 小米路由器,操作盘太卡 } 开发中用专业版 , 别用家庭版 比如有远程桌面程序 和 HV 虚拟机 查看激活信息 和 是不是永久激活 参考 h ...

  10. ScriptManager.RegisterStartupScript失效的解决方案

    在项目中一个页面使用System.Web.UI.ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success ...