Json.net[v3.5]对对象和JSON字符串的使用

前端之家收集整理的这篇文章主要介绍了Json.net[v3.5]对对象和JSON字符串的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Json.net[v3.5]对对象和JSON字符串的使用@H_404_5@

简介:Json.net[v3.5]对对象和JSON字符串的使用@H_404_5@

本文将介绍一个非常简单实用的例子,共享学习下@H_404_5@

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------@H_404_5@

以控制台应用程序为例子,我们需要在Program.cs中添加如下代码:@H_404_5@

    class Program
    {
        /// <summary>
        /// Json.net[v3.5]使用
        /// </summary>
        /// <author>PengZhen</author>
        /// <time>2013-11-5 21:29:27</time>
        /// <param name="args"></param>
        private static void Main(string[] args)
        {
            ApkList objApkList1 = new ApkList();
            objApkList1.ApkName = "ApkName123";
            objApkList1.version = "version321";
            string strJson1 = JsonConvert.SerializeObject(objApkList1);
            Console.WriteLine(strJson1);           
            Console.WriteLine("<-------------------------O(∩_∩)O------------------------->");

            string strDeJson1 = "{\"ApkName\":\"ApkName123\",\"version\":\"version321\"}";

            ApkList objApkList2 = new ApkList();
            objApkList2 = (ApkList) JsonConvert.DeserializeObject(strDeJson1,typeof (ApkList));

            Console.WriteLine("objApkList.ApkName:" + objApkList2.ApkName);

            Console.WriteLine("objApkList.version:" + objApkList2.version);

            Console.WriteLine("<-------------------------O(∩_∩)O------------------------->");

            List<ApkList> listApkList1 = new List<ApkList>();

            ApkList objApkList3 = new ApkList();
            objApkList3.ApkName = "1ApkName123";
            objApkList3.version = "1version321";
            listApkList1.Add(objApkList3);

            ApkList objApkList4 = new ApkList();
            objApkList4.ApkName = "2ApkName123";
            objApkList4.version = "2version321";
            listApkList1.Add(objApkList4);

            string strJson = JsonConvert.SerializeObject(listApkList1);
            Console.WriteLine(strJson);

            Console.WriteLine("<-------------------------O(∩_∩)O------------------------->");

            string strDeJson =
                "[{\"ApkName\":\"1\",\"version\":\"游戏\"},{\"ApkName\":\"2\",\"version\":\"书籍\"},{\"ApkName\":\"20\",\"version\":\"工具\"}]";

            List<ApkList> listApkList = (List<ApkList>) JsonConvert.DeserializeObject(strDeJson,typeof (List<ApkList>));

            ApkList objApkList5 = listApkList[0];
            Console.WriteLine("objApkList.ApkName:" + objApkList5.ApkName);

            Console.WriteLine("objApkList.version:" + objApkList5.version);


            ApkList objApkList6 = listApkList[1];
            Console.WriteLine("objApkList.ApkName:" + objApkList6.ApkName);

            Console.WriteLine("objApkList.version:" + objApkList6.version);


            ApkList objApkList7 = listApkList[2];
            Console.WriteLine("objApkList.ApkName:" + objApkList7.ApkName);

            Console.WriteLine("objApkList.version:" + objApkList7.version);


            Console.WriteLine("<-------------------------O(∩_∩)O------------------------->");

            Console.ReadLine();
        }

    }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------@H_404_5@

新建实体类:@H_404_5@

public class ApkList
{
    /// <summary>
    /// 包名
    /// </summary>
    private string _strApkName;
    /// <summary>
    /// 版本号
    /// </summary>
    private string _strAppVersion;
    /// <summary>
    /// 包名
    /// </summary>
    public string ApkName
    {
        get
        {
            return _strApkName;
        }
        set
        {
            _strApkName = value;
        }
    }
    /// <summary>
    /// 版本号
    /// </summary>
    public string version
    {
        get
        {
            return _strAppVersion;
        }
        set
        {
            _strAppVersion = value;
        }
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    public ApkList()
    {
        _strApkName = string.Empty;
        _strAppVersion = string.Empty;
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------@H_404_5@

程序运行效果图:@H_404_5@

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------@H_404_5@

Newtonsoft.Json.Net35下载地址 @H_404_5@

猜你在找的Json相关文章