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

本文通过实例演示了如何使用 Json.net v3.5 进行对象与 JSON 字符串之间的序列化与反序列化操作。包括单个对象及对象列表的处理方式。

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

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

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

    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();
        }

    }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

新建实体类:

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;
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

程序运行效果图:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Newtonsoft.Json.Net35下载地址
 

 


 

Json.NET 描述: json。 网络是一个流行的高性能JSON为。NET框架 灵活的JSON序列化器对之间的转换。净对象JSON linq到JSON用于手动阅读写作JSON 高性能,速度比。净的内置JSON序列化器 写缩进,容易阅读JSON JSONXML之间进行转换 支持: .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone and Windows 8 Store 版本: Json.NET has different libaries for the various .NET Framework versions. -Net45: .NET latest (4.5) -Net40: .NET 4.0 -Net35: .NET 3.5 -Net20: .NET 2.0 -WinRT: Windows 8 Store -Portable45: .NET 4.5, Windows Phone 8, Windows 8 Store -Portable40: .NET 4.0, Windows Phone 7, Windows 8 Store, Silverlight 4 Notes: Microsoft stopped support for the Compact Framework in Visual Studio 2010. For a Compact Framework 3.5 build download Json.NET 3.5. For a Silverlight 3.0 build download Json.NET 3.5. Microsoft Visual Studio 2010 重新生成解决方案的一些警告处理 警告 2 预定义类型“System.Action”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 3 预定义类型“System.Action”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 4 预定义类型“System.Action”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 5 预定义类型“System.Action”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 6 预定义类型“System.Func”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 7 预定义类型“System.Func”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 8 预定义类型“System.Func”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 9 预定义类型“System.Func”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 警告 10 预定义类型“System.Func”是在全局别名的多个程序集中定义的;将使用“c:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework \v4.0\mscorlib.dll”中的定义 ClassLibrary1 问题原因:检查程序发现,由于项目中引用了Newtonsoft.Json.Net20,从而造成系统的类重名(项目FRAMEWORK的版本4.0)。 Newtonsoft.Json.Net 包括: .NET 2, .NET 3.5, .NET 4, .NET 4.5, Silverlight, Windows Phone and Windows 8 Store,所有dll文件源码,有需要的同学可以直接下载。 个人网站多多支持:www.mlyuansu.com
Json.NET http://james.newtonking.com/projects/json-net.aspx http://www.codeplex.com/json/ Description: The Json.NET library makes working with JSON formatted data in .NET simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize your .NET objects with a single method call using the JsonSerializer. Json.NET&#39;s features include: - Lightning fast JsonReader and JsonWriter - The JsonSerializer for quickly converting your .NET objects to JSON and back again - Json.NET can optionally produce well formatted, indented JSON for debugging or display - Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized - Ability to convert JSON to and from XML Instructions: 1. Extract Newtonsoft.Json.dll and Newtonsoft.Json.xml from the archive&#39;s /bin directory into your own applications. 2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project. Silverlight: Json.NET has a build that works with the Silverlight .NET runtime. Reference the Newtonsoft.Json.Silverlight.dll from your Silverlight project. License: Copyright (c) 2007 James Newton-King Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值