csharp:.net 3.5 using System.Runtime.Serialization.Json read json

时间:2022-09-01 16:10:19
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&num; Serialization performance in System&period;Runtime&period;Serialization&period;Formatters&period;Binary&period;BinaryFormatter&comma;Newtonsoft&period;Json&period;JsonConvert and System&period;Text&period;Json&period;JsonSerializer&period;Serialize

    In .net core 3.0 using System;using System.Collections.Generic;using System.Collections;using System ...

  2. 找不到方法&colon;&OpenCurlyDoubleQuote;Boolean System&period;Runtime&period;Serialization&period;DataContractAttribute&period;get&lowbar;IsReference&lpar;&rpar;”的解决办法

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

  3. 重写成员&OpenCurlyDoubleQuote;log4net&period;Util&period;ReadOnlyPropertiesDictionary&period;GetObjectData&lpar;System&period;Runtime&period;Serialization&period;SerializationInfo&comma; System&period;Runtime&period;Serialization&period;StreamingContext&rpar;”时违反了继承安全性规则

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

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

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

  5. 引用System&period;Runtime&period;Serialization&period;Json

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

  6. 基础命名空间:序列化&lowbar;自定义序列化 System&period;Runtime&period;Serialization

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

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

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

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

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

  9. System&period;Runtime&period;Serialization&period;cs

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

随机推荐

  1. android EditText 默认情况下不获取焦点(不弹出输入框)

    可以在EditText前面放置一个看不到的LinearLayout,让它率先获取焦点: <LinearLayout android:focusable="true" andr ...

  2. MVC5 Bundles发布到IIS失效问题解决方案

    MVC中Bundles可以提高代码的可重用性 我每个页面都需要用到这十几个JS+CSS 当我把MVC发布到服务器以后,Bundles中的JS和CSS会失效的时候 宝宝的心里是崩溃的.... 查了很多资 ...

  3. PHP的优点

    1.语法简单 2.学习成本低 3.开发效率高 4.跨平台 5.开发部署方便 6.开源框架非常丰富(如:ThinkPHP) 7.开源CMS系统非常丰富(如:Joomla,Wordpress) 8.开源网 ...

  4. linux下磁盘分区

    转自于:http://pengyl.blog.51cto.com/5591604/1193963 命令:fdisk 功能:查看磁盘使用情况和分割磁盘 使用方法:                一.在 ...

  5. 一个仿3D的平面游戏页面

    package com.totoo.TouhouMassLight;import android.os.Bundle;import android.view.MotionEvent;import an ...

  6. 00-翻译IdentityServer4的目的

    强迫自己阅读英文文档 加深IdentityServer4的概念认识

  7. Confluence 6 如何配置快速导航的同时查找数量

    进入后台后查看快速导航的启用和可以同时查找的数量. 然后进行通过单击右上角的编辑(Edit)按钮进行编辑. 对配置进行配置,启用快速查询和可以同时使用的最大查询数量. https://www.cwik ...

  8. alpha冲刺1&sol;10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:来自双十一的爱 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 Alpha版本的任务细分安排 leangoo ...

  9. February 8th&comma; 2018 Week 6th Thursday

    When you fall in love, friends, let yourself fall. 当你坠入爱河,我的朋友,你就放手去爱吧. To love someone is like movi ...

  10. Find a way HDU - 2612

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...