JsonConvert ASP.NET Json的序列化与反序列化

来源:互联网 发布:全民wifi网络异常 编辑:程序博客网 时间:2024/06/05 00:43

首先在项目中引用库文件——Newtonsoft.Json.dll

JsonConvert.SerializeObject(object) 方法用于将对象序列化为json格式的字符串,object为对象参数。

JsonConvert.DeserializeObject(jsonStr)方法用于将json格式的字符串反序列化为json。jsonStr为json格式的字符串。

Newtonsoft.Json.Linq.JArray o = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(jsonStr);返回的是一个Newtonsoft.Json.Linq.JArray对象。类似于一个数组。

示例:

TCStep.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JsonSerializer_Test1
{
    public class TCStep
    {
        #region 私有数据成员
        private List<string> _inputs;
        private List<string> _expects;
        private string _exception;
        #endregion

        public TCStep()
        {
            _inputs = new List<string>();
            _expects = new List<string>();
            _exception = "";
        }

        public List<string> inputs
        {
            set { _inputs = value; }
            get { return _inputs; }
        }

        public List<string> expects
        {
            set { _expects = value; }
            get { return _expects; }
        }

        public string exception
        {
            set { _exception = value; }
            get { return _exception; }
        }
    }
}

 

testCase.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace JsonSerializer_Test1
{
    public class TestCase:IEnumerable
    {
        #region 私有数据成员
        private List<TCStep> _steps;
        #endregion

        public TestCase()
        {
            _steps = new List<TCStep>();
        }

        public TCStep this[int index]
        {
            get { return (TCStep)_steps[index]; }
            set { _steps[index] = value; }
        }

        public void Add(TCStep step)
        {
            _steps.Add(step);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            for (int i = 0; i < _steps.Count;i++ )
            {
                yield return _steps[i];
            }
        }
    }
}

 

testCases.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Collections;

namespace JsonSerializer_Test1
{
    public class TestCases:IEnumerable
    {
        #region 私有数据成员
        private List<TestCase> _testCases;
        #endregion

        public TestCases()
        {
            _testCases = new List<TestCase>();
        }

        public TestCase this[int index]
        {
            get { return (TestCase)_testCases[index]; }
            set { _testCases[index] = value; }
        }

        public void Add(TestCase tc)
        {
            _testCases.Add(tc);
        }

        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < _testCases.Count;i++ )
            {
                yield return _testCases[i];
            }
        }

    }
}

 

实例化TestCases对象:

#region 初始化step1
            List<string> step1_inputs = new List<string>();
            step1_inputs.Add("step1_input_1");
            step1_inputs.Add("step1_input_2");
            step1_inputs.Add("step1_input_3");

            List<string> step1_expects = new List<string>();
            step1_expects.Add("step1_expect_1");
            step1_expects.Add("step1_expect_2");
            step1_expects.Add("step1_expect_3");

            string step1_exception = "step_exception";

            TCStep _tcStep1 = new TCStep();
            _tcStep1.inputs = step1_inputs;
            _tcStep1.expects = step1_expects;
            _tcStep1.exception = step1_exception;
            #endregion

            #region 初始化step2
            List<string> step2_inputs = new List<string>();
            step2_inputs.Add("step2_input_1");
            step2_inputs.Add("step2_input_2");
            step2_inputs.Add("step2_input_3");

            List<string> step2_expects = new List<string>();
            step2_expects.Add("step2_expect_1");
            step2_expects.Add("step2_expect_2");
            step2_expects.Add("step2_expect_3");

            string step2_exception = "step_exception";

            TCStep _tcStep2 = new TCStep();
            _tcStep2.inputs = step2_inputs;
            _tcStep2.expects = step2_expects;
            _tcStep2.exception = step2_exception;
            #endregion

            #region 初始化step3
            List<string> step3_inputs = new List<string>();
            step3_inputs.Add("step3_input_1");
            step3_inputs.Add("step3_input_2");
            step3_inputs.Add("step3_input_3");

            List<string> step3_expects = new List<string>();
            step3_expects.Add("step3_expect_1");
            step3_expects.Add("step3_expect_2");
            step3_expects.Add("step3_expect_3");

            string step3_exception = "step_exception";

            TCStep _tcStep3 = new TCStep();
            _tcStep3.inputs = step3_inputs;
            _tcStep3.expects = step3_expects;
            _tcStep3.exception = step3_exception;
            #endregion

            #region 初始化step4
            List<string> step4_inputs = new List<string>();
            step4_inputs.Add("step4_input_1");
            step4_inputs.Add("step4_input_2");
            step4_inputs.Add("step4_input_3");

            List<string> step4_expects = new List<string>();
            step4_expects.Add("step4_expect_1");
            step4_expects.Add("step4_expect_2");
            step4_expects.Add("step4_expect_3");

            string step4_exception = "step_exception";

            TCStep _tcStep4 = new TCStep();
            _tcStep4.inputs = step4_inputs;
            _tcStep4.expects = step4_expects;
            _tcStep4.exception = step4_exception;
            #endregion

            #region 初始化step5
            List<string> step5_inputs = new List<string>();
            step5_inputs.Add("step5_input_1");
            step5_inputs.Add("step5_input_2");
            step5_inputs.Add("step5_input_3");

            List<string> step5_expects = new List<string>();
            step5_expects.Add("step5_expect_1");
            step5_expects.Add("step5_expect_2");
            step5_expects.Add("step5_expect_3");

            string step5_exception = "step_exception";

            TCStep _tcStep5 = new TCStep();
            _tcStep5.inputs = step5_inputs;
            _tcStep5.expects = step5_expects;
            _tcStep5.exception = step5_exception;
            #endregion

            #region 初始化testCase1

            TestCase testCase1 = new TestCase();
            testCase1.Add(_tcStep1);
            testCase1.Add(_tcStep2);
            testCase1.Add(_tcStep3);

            #endregion

            #region 初始化testCase2

            TestCase testCase2 = new TestCase();
            testCase2.Add(_tcStep4);
            testCase2.Add(_tcStep5);

            #endregion

            #region 初始化testCases

            TestCases _testCases = new TestCases();
            _testCases.Add(testCase1);
            _testCases.Add(testCase2);
            TestCases temp = _testCases;

            #endregion


//对象初始化说明:一个testcases里有n个testCase,一个testcase里有n个testStep

 

将testCases对象序列化为json字符串:

string json=JsonConvert.SerializeObject(_testCases);

 string path = @"F:/temp1.txt";
            FileStream fs = File.Create(path);
            fs.Close();
            fs.Dispose();

            StreamWriter sw = new StreamWriter(path,true, System.Text.Encoding.UTF8);
            sw.WriteLine(json);
            sw.Close();
            sw.Dispose();

 

将json字符串反序列化:

string path = @"F:/temp.txt";
            StreamReader sr = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312"));
            string jsonStr = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();

Newtonsoft.Json.Linq.JArray o = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(jsonStr);//testCases


            int temp = o.Count;

            var testCase1 = o[0];//TestCase1

            var testCase1_step1 = testCase1[0];//TCStep1
            var testCase1_step2 = testCase1[1];
            var testCase1_step3 = testCase1[2];

 

原创粉丝点击